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

Properly use proper subtyping for callables #16343

Merged
merged 1 commit into from Oct 27, 2023

Conversation

ilevkivskyi
Copy link
Member

Fixes #16338

This is kind of a major change, but it is technically correct: we should not treat (*args: Any, **kwargs: Any) special in is_proper_subtype() (only in is_subtype()). Unfortunately, this requires an additional flag for is_callable_compatible(), since currently we are passing the subtype kind information via a callback, which is not applicable to handling argument kinds.

@github-actions
Copy link
Contributor

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

porcupine (https://github.com/Akuli/porcupine): typechecking got 1.41x slower (30.7s -> 43.3s)
(Performance measurements are based on a single noisy sample)

comtypes (https://github.com/enthought/comtypes)
+ comtypes/client/_events.py:116: error: All conditional function variants must have identical signatures  [misc]
+ comtypes/client/_events.py:116: note: Original:
+ comtypes/client/_events.py:116: note:     def error_printer(self: Any, this: Any, *args: Any, **kw: Any) -> Any
+ comtypes/client/_events.py:116: note: Redefinition:
+ comtypes/client/_events.py:116: note:     def error_printer(*args: Any, **kw: Any) -> Any

discord.py (https://github.com/Rapptz/discord.py)
- discord/app_commands/commands.py:2381: error: Item "function" of "Callable[[Any, Interaction[Any], VarArg(Any), KwArg(Any)], Coroutine[Any, Any, Any]] | Callable[[Interaction[Any], VarArg(Any), KwArg(Any)], Coroutine[Any, Any, Any]]" has no attribute "__discord_app_commands_checks__"  [union-attr]
+ discord/app_commands/commands.py:2381: error: Item "function" of "Callable[[Any, Interaction[Any], VarArg(Any), KwArg(Any)], Coroutine[Any, Any, Any]] | Callable[[Interaction[Any], VarArg(Any), KwArg(Any)], Coroutine[Any, Any, Any]] | Callable[[Interaction[Any], Member], Coroutine[Any, Any, Any]] | Callable[[Interaction[Any], User], Coroutine[Any, Any, Any]] | Callable[[Interaction[Any], Message], Coroutine[Any, Any, Any]]" has no attribute "__discord_app_commands_checks__"  [union-attr]
- discord/app_commands/commands.py:2383: error: Item "function" of "Callable[[Any, Interaction[Any], VarArg(Any), KwArg(Any)], Coroutine[Any, Any, Any]] | Callable[[Interaction[Any], VarArg(Any), KwArg(Any)], Coroutine[Any, Any, Any]]" has no attribute "__discord_app_commands_checks__"  [union-attr]
+ discord/app_commands/commands.py:2383: error: Item "function" of "Callable[[Any, Interaction[Any], VarArg(Any), KwArg(Any)], Coroutine[Any, Any, Any]] | Callable[[Interaction[Any], VarArg(Any), KwArg(Any)], Coroutine[Any, Any, Any]] | Callable[[Interaction[Any], Member], Coroutine[Any, Any, Any]] | Callable[[Interaction[Any], User], Coroutine[Any, Any, Any]] | Callable[[Interaction[Any], Message], Coroutine[Any, Any, Any]]" has no attribute "__discord_app_commands_checks__"  [union-attr]

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Again, thanks for the quick fix! It's interesting that this wasn't noticed earlier.

The impact doesn't look huge based on mypy_primer, though can you have a look at the output from comtypes before merging?

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

comtypes primer hit looks correct, reveal_type(func(spam)) in the test changing seems a little unfortunate

@ilevkivskyi
Copy link
Member Author

Yes, the new error in mypy_primer is correct.

@hauntsaninja The Any in overload test actually doesn't worry me. This is quite a weird test, if you look at the commit history for this test, you will see something changes there quite often. TBH I am not even sure what this test is supposed to test, and what is a desired result there.

@ilevkivskyi ilevkivskyi merged commit 5c6ca5c into python:master Oct 27, 2023
18 checks passed
@ilevkivskyi ilevkivskyi deleted the fix-any-callable branch October 27, 2023 22:58
AlexWaygood added a commit to AlexWaygood/mypy that referenced this pull request Oct 27, 2023
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.

Regression: false positive about overload item never being matched
3 participants