-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Improve error messages for unexpected keyword arguments in overloaded functions #20592
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
Open
KevinRK29
wants to merge
11
commits into
python:master
Choose a base branch
from
KevinRK29:improve-overloaded-error-messages
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+285
−1
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
04ed0c5
improve error message for overloaded function
KevinRK29 29967dd
added line number to error message
KevinRK29 950f9aa
added fallback to list of possible overload variants
KevinRK29 d05a996
formatting
KevinRK29 51db719
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c903b30
revert line number change
KevinRK29 75e0a26
add tests
KevinRK29 34aeea1
merge kwargs without suggestions into one error line
KevinRK29 f3067fd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1ec21b5
split test into separate isolated test cases
KevinRK29 7675c99
added tests
KevinRK29 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2582,3 +2582,190 @@ def last_known_value() -> None: | |
| x, y, z = xy # E: Unpacking a string is disallowed | ||
| reveal_type(z) # N: Revealed type is "builtins.str" | ||
| [builtins fixtures/primitives.pyi] | ||
|
|
||
| [case testOverloadUnexpectedKeywordArgWithTypoSuggestion] | ||
| from typing import overload, Union | ||
|
|
||
| @overload | ||
| def f(foobar: int) -> None: ... | ||
|
|
||
| @overload | ||
| def f(foobar: str) -> None: ... | ||
|
|
||
| def f(foobar: Union[int, str]) -> None: pass | ||
|
|
||
| f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f"; did you mean "foobar"? | ||
| [builtins fixtures/list.pyi] | ||
|
|
||
| [case testOverloadUnexpectedKeywordArgNoMatch] | ||
| from typing import overload, Union | ||
|
|
||
| @overload | ||
| def f(foobar: int) -> None: ... | ||
|
|
||
| @overload | ||
| def f(foobar: str) -> None: ... | ||
|
|
||
| def f(foobar: Union[int, str]) -> None: pass | ||
|
|
||
| f(random=[1,2,3]) # E: Unexpected keyword argument "random" for overloaded function "f" \ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additional test ideas:
|
||
| # N: Possible overload variants: \ | ||
| # N: def f(foobar: int) -> None \ | ||
| # N: def f(foobar: str) -> None \ | ||
| # E: No overload variant of "f" matches argument type "list[int]" | ||
| [builtins fixtures/list.pyi] | ||
|
|
||
| [case testOverloadMultipleUnexpectedKeywordArgs] | ||
| from typing import overload, Union | ||
|
|
||
| @overload | ||
| def f(foobar: int) -> None: ... | ||
|
|
||
| @overload | ||
| def f(foobar: str) -> None: ... | ||
|
|
||
| def f(foobar: Union[int, str]) -> None: pass | ||
|
|
||
| f(fobar=1, baz=2) # E: Unexpected keyword argument "fobar" for overloaded function "f"; did you mean "foobar"? \ | ||
| # E: Unexpected keyword argument "baz" for overloaded function "f" | ||
| [builtins fixtures/list.pyi] | ||
|
|
||
| [case testOverloadManyUnexpectedKeywordArgs] | ||
| from typing import overload, Union | ||
|
|
||
| @overload | ||
| def f(foobar: int) -> None: ... | ||
|
|
||
| @overload | ||
| def f(foobar: str) -> None: ... | ||
|
|
||
| def f(foobar: Union[int, str]) -> None: pass | ||
|
|
||
| f(foobar=1, a=2, b=3, c=4, d=5, e=6) # E: Unexpected keyword arguments "a", "b", "c", "d", "e" for overloaded function "f" \ | ||
| # E: No overload variant of "f" matches argument types "int", "int", "int", "int", "int", "int" \ | ||
| # N: Possible overload variants: \ | ||
| # N: def f(foobar: int) -> None \ | ||
| # N: def f(foobar: str) -> None | ||
| [builtins fixtures/list.pyi] | ||
|
|
||
| [case testOverloadUnexpectedKeywordArgsWithTypeMismatch] | ||
| from typing import overload, Union | ||
|
|
||
| @overload | ||
| def f(foobar: int) -> None: ... | ||
|
|
||
| @overload | ||
| def f(foobar: str) -> None: ... | ||
|
|
||
| def f(foobar: Union[int, str]) -> None: pass | ||
|
|
||
| f(fobar=1, other=[1,2,3]) # E: Unexpected keyword argument "fobar" for overloaded function "f"; did you mean "foobar"? \ | ||
| # E: Unexpected keyword argument "other" for overloaded function "f" \ | ||
| # N: Possible overload variants: \ | ||
| # N: def f(foobar: int) -> None \ | ||
| # N: def f(foobar: str) -> None \ | ||
| # E: No overload variant of "f" matches argument types "int", "list[int]" | ||
| [builtins fixtures/list.pyi] | ||
|
|
||
| [case testOverloadPositionalArgTypeMismatch] | ||
| from typing import overload, Union | ||
|
|
||
| @overload | ||
| def g(x: int, y: int) -> int: ... | ||
|
|
||
| @overload | ||
| def g(x: str, y: str) -> str: ... | ||
|
|
||
| def g(x: Union[int, str], y: Union[int, str]) -> Union[int, str]: | ||
| return x | ||
|
|
||
| g([1, 2], 3) # E: No overload variant of "g" matches argument types "list[int]", "int" \ | ||
| # N: Possible overload variants: \ | ||
| # N: def g(x: int, y: int) -> int \ | ||
| # N: def g(x: str, y: str) -> str | ||
| [builtins fixtures/list.pyi] | ||
|
|
||
| [case testOverloadUnexpectedKeywordWithPositionalMismatch] | ||
| from typing import overload, Union | ||
|
|
||
| @overload | ||
| def g(x: int, y: int) -> int: ... | ||
|
|
||
| @overload | ||
| def g(x: str, y: str) -> str: ... | ||
|
|
||
| def g(x: Union[int, str], y: Union[int, str]) -> Union[int, str]: | ||
| return x | ||
|
|
||
| g([1, 2], z=3) # E: Unexpected keyword argument "z" for overloaded function "g" \ | ||
| # E: No overload variant of "g" matches argument types "list[int]", "int" \ | ||
| # N: Possible overload variants: \ | ||
| # N: def g(x: int, y: int) -> int \ | ||
| # N: def g(x: str, y: str) -> str | ||
| [builtins fixtures/list.pyi] | ||
|
|
||
| [case testOverloadNamedArgTypeMismatch] | ||
| from typing import overload, Union | ||
|
|
||
| @overload | ||
| def g(x: int, y: int) -> int: ... | ||
|
|
||
| @overload | ||
| def g(x: str, y: str) -> str: ... | ||
|
|
||
| def g(x: Union[int, str], y: Union[int, str]) -> Union[int, str]: | ||
| return x | ||
|
|
||
| g(x="hello", y=1) # E: No overload variant of "g" matches argument types "str", "int" \ | ||
| # N: Possible overload variants: \ | ||
| # N: def g(x: int, y: int) -> int \ | ||
| # N: def g(x: str, y: str) -> str | ||
| [builtins fixtures/list.pyi] | ||
|
|
||
| [case testOverloadMixedArgsWithNonFirstKeywordTypo] | ||
| from typing import overload, Union | ||
|
|
||
| @overload | ||
| def f(x: int, name: int, value: int) -> None: ... | ||
|
|
||
| @overload | ||
| def f(x: str, name: str, value: str) -> None: ... | ||
|
|
||
| def f(x: Union[int, str], name: Union[int, str], value: Union[int, str]) -> None: pass | ||
|
|
||
| f(1, name=2, valeu=3) # E: Unexpected keyword argument "valeu" for overloaded function "f"; did you mean "value"? \ | ||
| # E: No overload variant of "f" matches argument types "int", "int", "int" \ | ||
| # N: Possible overload variants: \ | ||
| # N: def f(x: int, name: int, value: int) -> None \ | ||
| # N: def f(x: str, name: str, value: str) -> None | ||
| [builtins fixtures/list.pyi] | ||
|
|
||
| [case testOverloadTwoMisspelledKeywordsDifferentTargets] | ||
| from typing import overload, Union | ||
|
|
||
| @overload | ||
| def f(name: int, value: int) -> None: ... | ||
|
|
||
| @overload | ||
| def f(name: str, value: str) -> None: ... | ||
|
|
||
| def f(name: Union[int, str], value: Union[int, str]) -> None: pass | ||
|
|
||
| f(nme=1, valeu=2) # E: Unexpected keyword argument "nme" for overloaded function "f"; did you mean "name"? \ | ||
| # E: Unexpected keyword argument "valeu" for overloaded function "f"; did you mean "value"? | ||
| [builtins fixtures/list.pyi] | ||
|
|
||
| [case testOverloadMethodKeywordTypo] | ||
| from typing import overload, Union | ||
|
|
||
| class A: | ||
| @overload | ||
| def f(self, foobar: int) -> None: ... | ||
|
|
||
| @overload | ||
| def f(self, foobar: str) -> None: ... | ||
|
|
||
| def f(self, foobar: Union[int, str]) -> None: pass | ||
|
|
||
| A().f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f" of "A"; did you mean "foobar"? | ||
| [builtins fixtures/list.pyi] | ||
Oops, something went wrong.
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.
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.
Additional test case ideas:
obj.f(fobar=1), where we have a misspelling in a method call.