-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Fix: Improve pipeline chaining type hints in typing.py #3919
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -30,7 +30,7 @@ | |||||
| PatternT = _StringLikeT # Patterns matched against keys, fields etc | ||||||
| FieldT = EncodableT # Fields within hash tables, streams and geo commands | ||||||
| KeysT = Union[KeyT, Iterable[KeyT]] | ||||||
| ResponseT = Union[Awaitable[Any], Any] | ||||||
| ResponseT = Union[Awaitable[Any], Any, "PipelineCommandsProtocol"] | ||||||
| ChannelT = _StringLikeT | ||||||
| GroupT = _StringLikeT # Consumer group | ||||||
| ConsumerT = _StringLikeT # Consumer name | ||||||
|
|
@@ -53,5 +53,11 @@ class CommandsProtocol(Protocol): | |||||
| def execute_command(self, *args, **options) -> ResponseT: ... | ||||||
|
|
||||||
|
|
||||||
| class PipelineCommandsProtocol(Protocol): | ||||||
| def execute_command(self, *args, **options) -> ResponseT: ... | ||||||
|
||||||
| def execute_command(self, *args, **options) -> ResponseT: ... | |
| def execute_command(self, *args, **options) -> Any: ... |
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
PipelineCommandsProtocoltype is added to theResponseTunion, but this creates a semantic issue.ResponseTis used as the return type for all Redis command methods (e.g.,set(),get()). AddingPipelineCommandsProtocolto this union means that when callingredis_client.get("key"), the type checker will infer the return type could be aPipelineCommandsProtocol, which is incorrect - a regular Redis client should never return a Pipeline.The underlying issue is that command methods are shared between
RedisandPipelineclasses, but they should have different return types. A more appropriate solution would involve using TypeVars or Self types to properly model this polymorphism, rather than adding Pipeline to the general response type union.