-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Improve error messages when positional arguments are missing #20591
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
139b2a9
7745bd2
04a8121
6c2bfdd
8fc27d5
f307c28
c59e4f9
3340653
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 |
|---|---|---|
|
|
@@ -3703,3 +3703,74 @@ foo(*args) # E: Argument 1 to "foo" has incompatible type "*list[object]"; expe | |
| kwargs: dict[str, object] | ||
| foo(**kwargs) # E: Argument 1 to "foo" has incompatible type "**dict[str, object]"; expected "P" | ||
| [builtins fixtures/dict.pyi] | ||
|
|
||
| [case testMissingPositionalArgShiftDetectedMiddle] | ||
| def f(x: int, y: str, z: bytes, aa: int) -> None: ... | ||
|
|
||
| f(1, b'x', 1) | ||
| [builtins fixtures/primitives.pyi] | ||
| [out] | ||
| main:3: error: Missing positional argument "y" in call to "f" | ||
|
|
||
| [case testMissingPositionalArgShiftDetectedFirst] | ||
| def f(x: int, y: str, z: bytes) -> None: ... | ||
|
|
||
| f("hello", b'x') | ||
| [builtins fixtures/primitives.pyi] | ||
| [out] | ||
| main:3: error: Missing positional argument "x" in call to "f" | ||
|
|
||
| [case testMissingPositionalArgShiftDetectedManyArgs] | ||
| def f(a: int, b: str, c: float, d: list[int], e: tuple[str, ...]) -> None: ... | ||
|
|
||
| f(1, 1.5, [1, 2, 3], ("a", "b")) | ||
| [builtins fixtures/list.pyi] | ||
| [out] | ||
| main:3: error: Missing positional argument "b" in call to "f" | ||
|
|
||
| [case testMissingPositionalArgShiftDetectedLast] | ||
| def f(x: int, y: str, z: bytes) -> None: ... | ||
|
|
||
| f(1, "hello") | ||
| [builtins fixtures/primitives.pyi] | ||
| [out] | ||
| main:3: error: Missing positional argument "z" in call to "f" | ||
|
|
||
| [case testMissingPositionalArgNoShiftPattern] | ||
| def f(x: int, y: str, z: bytes) -> None: ... | ||
|
|
||
| f("wrong", 123) | ||
| [builtins fixtures/primitives.pyi] | ||
| [out] | ||
| main:3: error: Missing positional argument "z" in call to "f" | ||
| main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int" | ||
| main:3: error: Argument 2 to "f" has incompatible type "int"; expected "str" | ||
|
|
||
| [case testMissingPositionalArgMultipleMissing] | ||
| def f(a: int, b: str, c: float, d: list[int]) -> None: ... | ||
|
|
||
| f(1.5, [1, 2, 3]) | ||
| [builtins fixtures/list.pyi] | ||
| [out] | ||
| main:3: error: Missing positional arguments "c", "d" in call to "f" | ||
| main:3: error: Argument 1 to "f" has incompatible type "float"; expected "int" | ||
| main:3: error: Argument 2 to "f" has incompatible type "list[int]"; expected "str" | ||
|
|
||
| [case testMissingPositionalArgWithDefaults] | ||
| def f(x: int, y: str, z: bytes = b'default') -> None: ... | ||
|
|
||
| f("hello") | ||
| [builtins fixtures/primitives.pyi] | ||
| [out] | ||
| main:3: error: Missing positional argument "y" in call to "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. Can you report this error after the 'Argument N to ...' errors, so that we'd report the errors in the same order as the arguments are in the call. |
||
| main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int" | ||
|
|
||
| [case testMissingPositionalArgWithStarArgs] | ||
| def f(x: int, y: str, z: bytes, *args: int) -> None: ... | ||
|
|
||
| f("hello", b'x') | ||
| [builtins fixtures/primitives.pyi] | ||
| [out] | ||
| main:3: error: Missing positional argument "z" in call to "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. In this case it's not clear what the idea error message would be, since the caller accepts |
||
| main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int" | ||
| main:3: error: Argument 2 to "f" has incompatible type "bytes"; expected "str" | ||
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.
Since there are multiple positional arguments missing, it seems reasonable to not try to align them, so this looks fine, but it would be clearer if the order of the errors would be different (see my other comments).