Basic support for decorated overloads#15898
Merged
ilevkivskyi merged 6 commits intopython:masterfrom Aug 18, 2023
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Member
Author
|
Just to confirm: new errors in |
hauntsaninja
approved these changes
Aug 18, 2023
| """ | ||
| callee = get_proper_type(callee) | ||
|
|
||
| overloaded = self.is_generic_decorator_overload_call(callee, args) |
Collaborator
There was a problem hiding this comment.
nit: check_call is pretty hot, should we move this inside if isinstance(callee, CallableType): on L1513?
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: Expression (https://github.com/cognitedata/Expression)
- expression/extra/parser.py:81: error: No overload variant of "starmap" matches argument type "Callable[..., Any]" [call-overload]
- expression/extra/parser.py:81: note: Possible overload variants:
- expression/extra/parser.py:81: note: def [_A, _B, _C] starmap(mapper: Callable[[_A, _B], _C], parser: Parser[tuple[_A, _B]]) -> Parser[_C]
- expression/extra/parser.py:81: note: def [_A, _B, _C, _D] starmap(mapper: Callable[[_A, _B, _C], _D], parser: Parser[tuple[_A, _B, _C]]) -> Parser[_D]
- expression/extra/parser.py:219: error: Overloaded function implementation does not accept all possible arguments of signature 1 [misc]
- expression/extra/parser.py:219: error: Overloaded function implementation does not accept all possible arguments of signature 2 [misc]
+ expression/extra/parser.py:435: error: Cannot infer type argument 3 of "pipe" [misc]
+ expression/extra/parser.py:440: error: Argument 1 to "starmap" has incompatible type "Callable[[Some[str] | Nothing_[str], Block[str]], int]"; expected "Callable[[_A, _B], _C]" [arg-type]
- expression/extra/parser.py:440: error: No overload variant of "starmap" matches argument type "Callable[[Some[str] | Nothing_[str], Block[str]], int]" [call-overload]
- expression/extra/parser.py:440: note: Possible overload variants:
- expression/extra/parser.py:440: note: def [_A, _B, _C] starmap(mapper: Callable[[_A, _B], _C], parser: Parser[tuple[_A, _B]]) -> Parser[_C]
- expression/extra/parser.py:440: note: def [_A, _B, _C, _D] starmap(mapper: Callable[[_A, _B, _C], _D], parser: Parser[tuple[_A, _B, _C]]) -> Parser[_D]
+ expression/extra/parser.py:469: error: Cannot infer type argument 2 of "pipe" [misc]
+ expression/extra/parser.py:480: error: Argument 1 to "starmap" has incompatible type "Callable[[tuple[Some[str] | Nothing_[str], Block[str]], Some[Block[str]] | Nothing_[Block[str]]], float]"; expected "Callable[[_A, _B], _C]" [arg-type]
- expression/extra/parser.py:480: error: No overload variant of "starmap" matches argument type "Callable[[tuple[Some[str] | Nothing_[str], Block[str]], Some[Block[str]] | Nothing_[Block[str]]], float]" [call-overload]
- expression/extra/parser.py:480: note: Possible overload variants:
- expression/extra/parser.py:480: note: def [_A, _B, _C] starmap(mapper: Callable[[_A, _B], _C], parser: Parser[tuple[_A, _B]]) -> Parser[_C]
- expression/extra/parser.py:480: note: def [_A, _B, _C, _D] starmap(mapper: Callable[[_A, _B, _C], _D], parser: Parser[tuple[_A, _B, _C]]) -> Parser[_D]
asynq (https://github.com/quora/asynq)
+ asynq/tools.pyi:42: error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [misc]
+ asynq/tools.pyi:48: error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [misc]
|
|
I'm eager to use this feature, since it resolves #15737. Any estimate on when it might be included in a release version of mypy, e.g. a 1.5.2 release? |
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #15737
Fixes #12844
Fixes #12716
My goal was to fix the
ParamSpecissues, but it turns out decorated overloads were not supported at all. Namely:Here I add basic support using same logic as for regular decorated functions: initially set type to
Noneand defer callers until definition is type-checked. Note this results in few moreCannot determine typein case of other errors, but I think it is fine.Note I also add special-casing for "inline" applications of generic functions to overload arguments. This use case was mentioned few times alongside overloads. The general fix would be tricky, and my special-casing should cover typical use cases.