Skip to content

Commit c967cdb

Browse files
committed
handle missing pos args at the end
1 parent f7b108f commit c967cdb

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

mypy/checkexpr.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,8 +2355,9 @@ def _detect_missing_positional_arg(
23552355
"""Try to identify a single missing positional argument using type alignment.
23562356
23572357
If the caller and callee are just positional arguments and exactly one arg is missing,
2358-
we scan left to right to find which argument skipped. If there is an error, report it
2359-
and return True, or return False to fall back to normal checking.
2358+
we scan left to right to find which argument skipped. If only the last argument is missing,
2359+
we return False since it's already handled in a desired manner. If there is an error,
2360+
report it and return True, or return False to fall back to normal checking.
23602361
"""
23612362
if not all(k == ARG_POS for k in callee.arg_kinds):
23622363
return False
@@ -2381,6 +2382,9 @@ def _detect_missing_positional_arg(
23812382
if skip_idx is None or j != len(arg_types):
23822383
return False
23832384

2385+
if skip_idx == len(callee.arg_types) - 1:
2386+
return False
2387+
23842388
param_name = callee.arg_names[skip_idx]
23852389
callee_name = callable_name(callee)
23862390
if param_name is None or callee_name is None:

test-data/unit/check-functions.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,6 +3810,22 @@ main:3: error: Missing positional argument "z" in call to "f"
38103810
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int"
38113811
main:3: error: Argument 2 to "f" has incompatible type "int"; expected "str"
38123812

3813+
[case testMissingPositionalArgNoShiftPatternLast]
3814+
def f(x: int, y: str, z: bytes) -> None: ...
3815+
3816+
f(123, "wrong")
3817+
[builtins fixtures/primitives.pyi]
3818+
[out]
3819+
main:3: error: Missing positional argument "z" in call to "f"
3820+
3821+
[case testMissingPositionalArgNoShiftPatternNone]
3822+
def f(x: int) -> None: ...
3823+
3824+
f()
3825+
[builtins fixtures/primitives.pyi]
3826+
[out]
3827+
main:3: error: Missing positional argument "x" in call to "f"
3828+
38133829
[case testMissingPositionalArgMultipleMissing]
38143830
def f(a: int, b: str, c: float, d: list[int]) -> None: ...
38153831

0 commit comments

Comments
 (0)