Skip to content
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

Use class name as namespace for type variables #12590

Merged
merged 8 commits into from
Apr 19, 2022
Merged

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Apr 14, 2022

This avoids confusion between type variables of two classes, which can
happen at least in some edge cases. Type variables are only the same if
both the numeric id and namespace match (plus meta level).

Fixes #12588 (though the textual presentation of type variables doesn't
take the namespace into consideration yet).

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

websockets (https://github.com/aaugustin/websockets)
+ src/websockets/legacy/protocol.py:687: error: Argument 1 to "__anext__" of "AsyncIterator" has incompatible type "AsyncIterator[_T_co]"; expected "AsyncIterator[_T_co]"

xarray (https://github.com/pydata/xarray)
- xarray/core/alignment.py:534: error: Argument 1 to "_reindex_one" of "Aligner" has incompatible type "Tuple[DataAlignable, Dict[Tuple[Tuple[Tuple[Hashable, Tuple[Hashable, ...]], ...], Type[Index]], Index]]"; expected "DataAlignable"  [arg-type]

Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/abc.py:2860: error: Signature of "copy" incompatible with supertype "ExecutableCommand"  [override]
- tanjun/abc.py:2860: note:      Superclass:
- tanjun/abc.py:2860: note:          def copy(self) -> MessageCommand[MessageContext]
- tanjun/abc.py:2860: note:      Subclass:
- tanjun/abc.py:2860: note:          def copy(self, *, parent: Optional[MessageCommandGroup[Any]] = ...) -> MessageCommand[_CommandCallbackSigT]
- tanjun/commands/slash.py:1272: error: Return type "SlashCommand[_CommandCallbackSigT]" of "bind_client" incompatible with return type "SlashCommand[SlashContext]" in supertype "PartialCommand"  [override]
- tanjun/commands/slash.py:1272: error: Return type "SlashCommand[_CommandCallbackSigT]" of "bind_client" incompatible with return type "SlashCommand[SlashContext]" in supertype "ExecutableCommand"  [override]
- tanjun/commands/slash.py:2454: error: Signature of "copy" incompatible with supertype "PartialCommand"  [override]
- tanjun/commands/slash.py:2454: note:      Superclass:
- tanjun/commands/slash.py:2454: note:          def copy(self, *, _new: bool = ...) -> SlashCommand[SlashContext]
- tanjun/commands/slash.py:2454: note:      Subclass:
- tanjun/commands/slash.py:2454: note:          def copy(self, *, _new: bool = ..., parent: Optional[SlashCommandGroup] = ...) -> SlashCommand[_CommandCallbackSigT]
- tanjun/commands/slash.py:2454: error: Signature of "copy" incompatible with supertype "ExecutableCommand"  [override]
- tanjun/commands/slash.py:2454: note:          def copy(self) -> SlashCommand[SlashContext]
- tanjun/commands/slash.py:2454: note:          def copy(self, *, _new: bool = ..., parent: Optional[SlashCommandGroup] = ...) -> SlashCommand[_CommandCallbackSigT]
- tanjun/commands/message.py:249: error: Return type "MessageCommand[_CommandCallbackSigT]" of "bind_client" incompatible with return type "MessageCommand[MessageContext]" in supertype "PartialCommand"  [override]
- tanjun/commands/message.py:249: error: Return type "MessageCommand[_CommandCallbackSigT]" of "bind_client" incompatible with return type "MessageCommand[MessageContext]" in supertype "ExecutableCommand"  [override]
- tanjun/commands/message.py:257: error: Return type "MessageCommand[_CommandCallbackSigT]" of "bind_component" incompatible with return type "MessageCommand[MessageContext]" in supertype "PartialCommand"  [override]
- tanjun/commands/message.py:257: error: Return type "MessageCommand[_CommandCallbackSigT]" of "bind_component" incompatible with return type "MessageCommand[MessageContext]" in supertype "ExecutableCommand"  [override]
- tanjun/commands/message.py:265: error: Signature of "copy" incompatible with supertype "PartialCommand"  [override]
- tanjun/commands/message.py:265: note:      Superclass:
- tanjun/commands/message.py:265: note:          def copy(self, *, _new: bool = ...) -> MessageCommand[MessageContext]
- tanjun/commands/message.py:265: note:      Subclass:
- tanjun/commands/message.py:265: note:          def copy(self, *, parent: Optional[MessageCommandGroup[Any]] = ..., _new: bool = ...) -> MessageCommand[_CommandCallbackSigT]
- tanjun/commands/message.py:265: error: Signature of "copy" incompatible with supertype "ExecutableCommand"  [override]
- tanjun/commands/message.py:265: note:          def copy(self) -> MessageCommand[MessageContext]
- tanjun/commands/message.py:265: note:          def copy(self, *, parent: Optional[MessageCommandGroup[Any]] = ..., _new: bool = ...) -> MessageCommand[_CommandCallbackSigT]
- tanjun/commands/message.py:423: error: Signature of "copy" incompatible with supertype "PartialCommand"  [override]
- tanjun/commands/message.py:423: note:      Superclass:
- tanjun/commands/message.py:423: note:          def copy(self, *, _new: bool = ...) -> MessageCommandGroup[MessageContext]
- tanjun/commands/message.py:423: note:      Subclass:
- tanjun/commands/message.py:423: note:          def copy(self, *, parent: Optional[MessageCommandGroup[Any]] = ..., _new: bool = ...) -> MessageCommandGroup[_CommandCallbackSigT]
- tanjun/commands/message.py:423: error: Signature of "copy" incompatible with supertype "ExecutableCommand"  [override]
- tanjun/commands/message.py:423: note:          def copy(self) -> MessageCommandGroup[MessageContext]
- tanjun/commands/message.py:423: note:          def copy(self, *, parent: Optional[MessageCommandGroup[Any]] = ..., _new: bool = ...) -> MessageCommandGroup[_CommandCallbackSigT]
- tanjun/commands/message.py:493: error: Return type "MessageCommandGroup[_CommandCallbackSigT]" of "bind_client" incompatible with return type "MessageCommandGroup[MessageContext]" in supertype "PartialCommand"  [override]
- tanjun/commands/message.py:493: error: Return type "MessageCommandGroup[_CommandCallbackSigT]" of "bind_client" incompatible with return type "MessageCommandGroup[MessageContext]" in supertype "ExecutableCommand"  [override]
- tanjun/commands/message.py:501: error: Return type "MessageCommandGroup[_CommandCallbackSigT]" of "bind_component" incompatible with return type "MessageCommandGroup[MessageContext]" in supertype "PartialCommand"  [override]
- tanjun/commands/message.py:501: error: Return type "MessageCommandGroup[_CommandCallbackSigT]" of "bind_component" incompatible with return type "MessageCommandGroup[MessageContext]" in supertype "ExecutableCommand"  [override]
- tanjun/commands/menu.py:448: error: Return type "MenuCommand[_MenuCommandCallbackSigT, _MenuTypeT]" of "set_tracked_command" incompatible with return type "MenuCommand[MenuContext, _MenuTypeT]" in supertype "AppCommand"  [override]
- tanjun/commands/menu.py:484: error: Signature of "copy" incompatible with supertype "PartialCommand"  [override]
- tanjun/commands/menu.py:484: note:      Superclass:
- tanjun/commands/menu.py:484: note:          def copy(self, *, _new: bool = ...) -> MenuCommand[MenuContext, _MenuTypeT]
- tanjun/commands/menu.py:484: note:      Subclass:
- tanjun/commands/menu.py:484: note:          def copy(self, *, _new: bool = ..., parent: Optional[SlashCommandGroup] = ...) -> MenuCommand[_MenuCommandCallbackSigT, _MenuTypeT]
- tanjun/commands/menu.py:484: error: Signature of "copy" incompatible with supertype "ExecutableCommand"  [override]
- tanjun/commands/menu.py:484: note:          def copy(self) -> MenuCommand[MenuContext, _MenuTypeT]
- tanjun/commands/menu.py:484: note:          def copy(self, *, _new: bool = ..., parent: Optional[SlashCommandGroup] = ...) -> MenuCommand[_MenuCommandCallbackSigT, _MenuTypeT]

@JelleZijlstra
Copy link
Member

The mypy-primer output looks suspicious, though I haven't investigated in detail. This error message won't be fun for anyone: + src/websockets/legacy/protocol.py:687: error: Argument 1 to "__anext__" of "AsyncIterator" has incompatible type "AsyncIterator[_T_co]"; expected "AsyncIterator[_T_co]".

@hauntsaninja
Copy link
Collaborator

cc @A5rocks since this PR accomplishes similar things as #11657

Copy link
Contributor

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this namespacing is a better approach than my own PR (which essentially mutated the typevars on an existing class to be at least larger than every other in its inheritance tree). Should its test also be carried over, though?

@JukkaL
Copy link
Collaborator Author

JukkaL commented Apr 19, 2022

It looks like websockets didn't have an error more or less accidentally before (one mypy bug hid another mypy bug). Mypy infers types with unbound type variables (which it shouldn't do), and previously two types with unbound type variables were considered compatible, whereas now there's an error. I'm going to try to improve the error message in a follow-up PR, however (or fix the leaking unbound type variables).

@A5rocks I'll add the test case in a follow-up PR.

@JukkaL JukkaL merged commit 20b0b9b into master Apr 19, 2022
@JukkaL JukkaL deleted the tvar-namespacing branch April 19, 2022 10:50
JukkaL added a commit that referenced this pull request Apr 19, 2022
This avoids confusion between type variables of two classes, which can
happen at least in some edge cases. Type variables are only the same if
both the numeric id and namespace match (plus meta level).

Fixes #12588 (though the textual presentation of type variables doesn't
take the namespace into consideration yet).
JukkaL added a commit that referenced this pull request Apr 19, 2022
This was originally written by @A5rocks in #11657.

Related to #12590.
This was referenced Apr 19, 2022
hauntsaninja pushed a commit that referenced this pull request Apr 20, 2022
This was originally written by @A5rocks in #11657.

Related to #12590.
cdce8p added a commit to cdce8p/mypy that referenced this pull request Apr 21, 2022
cdce8p added a commit to cdce8p/mypy that referenced this pull request Apr 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid type inference for zip in a generic class
5 participants