-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Improved type annotations #3619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
136b0a2
to
b6d4a89
Compare
Hi @mahenzon, thank you for your contribution. We will go over it soon. |
def0478
to
38eb7c3
Compare
38eb7c3
to
de3cd7e
Compare
Would this solve #3574? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves type annotations across the codebase to ensure that both synchronous and asynchronous Redis clients report more precise return types that are compatible with static type checkers. Key changes include the introduction of new type variables in redis/typing.py, refined method return types in redis/commands/core.py (including generic-based command classes), and updates to command parser mappings in redis/_parsers/helpers.py.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
redis/typing.py | Added new type definitions and ResponseType* type variables |
redis/commands/helpers.py | Updated function signature for list_or_args to support Optional args |
redis/commands/core.py | Refactored return types for various command methods using generics |
redis/_parsers/helpers.py | Adjusted SMISMEMBER parser entries |
Awaitable[List[Union[Literal[0], Literal[1]]]], | ||
List[Union[Literal[0], Literal[1]]], | ||
]: | ||
def smismember(self, name: str, values: List, *args: List) -> ResponseTypeBoolean: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type for smismember is annotated as ResponseTypeBoolean but the underlying parser returns a list of booleans. Consider defining and using a dedicated type (e.g., ResponseTypeBooleanList) to accurately reflect the returned value.
Copilot uses AI. Check for mistakes.
@@ -777,6 +777,7 @@ def string_keys_to_dict(key_string, callback): | |||
"SENTINEL SET": bool_ok, | |||
"SLOWLOG GET": parse_slowlog_get, | |||
"SLOWLOG RESET": bool_ok, | |||
"SMISMEMBER": lambda r: list(map(bool, r)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Duplicate entries for the SMISMEMBER key are added in this dictionary. Consolidating these duplicates could improve maintainability and clarity.
Copilot uses AI. Check for mistakes.
Pull Request check-list
Reviewed and checked all of these items:
Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?- no API changeschecks.py
file example to the repo?Description of change
Hello, there're some issues with type annotations: #2399 #3169
Example:
We can see that sync redis client may return awaitable result, but it will never do.
This is made for compatibility with async redis, but it introduces some challenges when checking code with static type checkers like mypy.
Also it's
Any
instead ofstr
orbytes
because we can't predict, ifdecode_responses
isTrue
orFalse
- it'll be addressed later.I'd like to make it work this way:
I started reworking annotations, so type annotations for sync / async redis work as expected - sync redis doesn't return
Awaitable
, async redis returnsAwaitable
.Important
The goal is not to make the whole
redis-py
source code mypy-comliant and fully annotated.The goal is to make usage of
redis-py
in python projects compatible with mypy - make return type annotations more precise. So devs don't need to addcast
s andassert
s to their code.Example code. where I checked new typing
File:
checks.py
:I checked it with mypy with
strict=True
enabled:mypy --strict checks.py
, added comments to the code example above.If these changes are ok for
redis-py
, I'll continue working on the update.There's one major issue: these annotations are ok when
decode_responses=True
, but if not passed, ordecode_responses=False
, then some methods will returnbytes
instead ofstr
- I'm working on a solution for this.Also I fixed parsing for some commands which should return bools: 23ff994