Skip to content

Commit f307c28

Browse files
committed
made error messages more consistent and improved test names to not include implementation details
1 parent 8fc27d5 commit f307c28

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

mypy/checkexpr.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,8 +2398,7 @@ def check_call_arguments(
23982398
)
23992399
self.msg.fail(
24002400
f'Argument {shift_position} to "{func_name}" has incompatible type '
2401-
f"{actual_str}; expected {expected_str} "
2402-
f'(did you forget argument "{param_name}"?)',
2401+
f"{actual_str}; expected {expected_str}",
24032402
context,
24042403
code=codes.CALL_ARG,
24052404
)
@@ -2491,13 +2490,15 @@ def detect_shifted_positional_args(
24912490
if not missing_positional:
24922491
return None
24932492

2493+
# Only attempt shift detection when exactly one argument is missing.
2494+
# When multiple arguments are missing, we should fall back to the original behavior.
2495+
if len(missing_positional) != 1:
2496+
return None
2497+
24942498
has_star_args = any(k == nodes.ARG_STAR for k in callee.arg_kinds)
24952499
has_star_kwargs = any(k == nodes.ARG_STAR2 for k in callee.arg_kinds)
24962500
has_defaults = any(k == nodes.ARG_OPT for k in callee.arg_kinds)
2497-
single_missing = len(missing_positional) == 1
2498-
high_confidence = (
2499-
single_missing and not has_star_args and not has_star_kwargs and not has_defaults
2500-
)
2501+
high_confidence = not has_star_args and not has_star_kwargs and not has_defaults
25012502

25022503
positional_actual_types = [
25032504
actual_types[i] for i, k in enumerate(actual_kinds) if k == nodes.ARG_POS

test-data/unit/check-functions.test

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3704,23 +3704,31 @@ kwargs: dict[str, object]
37043704
foo(**kwargs) # E: Argument 1 to "foo" has incompatible type "**dict[str, object]"; expected "P"
37053705
[builtins fixtures/dict.pyi]
37063706

3707-
[case testMissingPositionalArgumentShiftedTypes]
3707+
[case testMissingPositionalArgumentTypeMismatch]
37083708
def f(x: int, y: str, z: bytes, aa: int) -> None: ...
37093709

37103710
f(1, b'x', 1)
37113711
[builtins fixtures/primitives.pyi]
37123712
[out]
3713-
main:3: error: Argument 2 to "f" has incompatible type "bytes"; expected "str" (did you forget argument "y"?)
3713+
main:3: error: Argument 2 to "f" has incompatible type "bytes"; expected "str"
37143714

3715-
[case testMissingPositionalArgumentShiftedTypesFirstArg]
3715+
[case testMissingPositionalArgumentTypeMismatchFirst]
37163716
def f(x: int, y: str, z: bytes) -> None: ...
37173717

37183718
f("hello", b'x')
37193719
[builtins fixtures/primitives.pyi]
37203720
[out]
3721-
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int" (did you forget argument "x"?)
3721+
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int"
3722+
3723+
[case testMissingPositionalArgumentManyArgs]
3724+
def f(a: int, b: str, c: float, d: list[int], e: tuple[str, ...]) -> None: ...
3725+
3726+
f(1, 1.5, [1, 2, 3], ("a", "b"))
3727+
[builtins fixtures/list.pyi]
3728+
[out]
3729+
main:3: error: Argument 2 to "f" has incompatible type "float"; expected "str"
37223730

3723-
[case testMissingPositionalArgumentNoShift]
3731+
[case testMissingPositionalArgumentNoPattern]
37243732
def f(x: int, y: str, z: bytes) -> None: ...
37253733

37263734
f("wrong", 123)
@@ -3730,15 +3738,17 @@ main:3: error: Missing positional argument "z" in call to "f"
37303738
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int"
37313739
main:3: error: Argument 2 to "f" has incompatible type "int"; expected "str"
37323740

3733-
[case testMissingPositionalArgumentShiftedTypesManyArgs]
3734-
def f(a: int, b: str, c: float, d: list[int], e: tuple[str, ...]) -> None: ...
3741+
[case testMissingMultiplePositionalArguments]
3742+
def f(a: int, b: str, c: float, d: list[int]) -> None: ...
37353743

3736-
f(1, 1.5, [1, 2, 3], ("a", "b"))
3744+
f(1.5, [1, 2, 3])
37373745
[builtins fixtures/list.pyi]
37383746
[out]
3739-
main:3: error: Argument 2 to "f" has incompatible type "float"; expected "str" (did you forget argument "b"?)
3747+
main:3: error: Missing positional arguments "c", "d" in call to "f"
3748+
main:3: error: Argument 1 to "f" has incompatible type "float"; expected "int"
3749+
main:3: error: Argument 2 to "f" has incompatible type "list[int]"; expected "str"
37403750

3741-
[case testMissingPositionalArgumentShiftedWithDefaults]
3751+
[case testMissingPositionalArgumentWithDefaults]
37423752
def f(x: int, y: str, z: bytes = b'default') -> None: ...
37433753

37443754
f("hello")
@@ -3747,7 +3757,7 @@ f("hello")
37473757
main:3: error: Missing positional argument "y" in call to "f"
37483758
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int"
37493759

3750-
[case testMissingPositionalArgumentShiftedWithStarArgs]
3760+
[case testMissingPositionalArgumentWithStarArgs]
37513761
def f(x: int, y: str, z: bytes, *args: int) -> None: ...
37523762

37533763
f("hello", b'x')

0 commit comments

Comments
 (0)