Skip to content

Commit a432f38

Browse files
committed
made error messages more consistent and improved test names to not include implementation details
1 parent 2c431a5 commit a432f38

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
@@ -2393,8 +2393,7 @@ def check_call_arguments(
23932393
)
23942394
self.msg.fail(
23952395
f'Argument {shift_position} to "{func_name}" has incompatible type '
2396-
f"{actual_str}; expected {expected_str} "
2397-
f'(did you forget argument "{param_name}"?)',
2396+
f"{actual_str}; expected {expected_str}",
23982397
context,
23992398
code=codes.CALL_ARG,
24002399
)
@@ -2486,13 +2485,15 @@ def detect_shifted_positional_args(
24862485
if not missing_positional:
24872486
return None
24882487

2488+
# Only attempt shift detection when exactly one argument is missing.
2489+
# When multiple arguments are missing, we should fall back to the original behavior.
2490+
if len(missing_positional) != 1:
2491+
return None
2492+
24892493
has_star_args = any(k == nodes.ARG_STAR for k in callee.arg_kinds)
24902494
has_star_kwargs = any(k == nodes.ARG_STAR2 for k in callee.arg_kinds)
24912495
has_defaults = any(k == nodes.ARG_OPT for k in callee.arg_kinds)
2492-
single_missing = len(missing_positional) == 1
2493-
high_confidence = (
2494-
single_missing and not has_star_args and not has_star_kwargs and not has_defaults
2495-
)
2496+
high_confidence = not has_star_args and not has_star_kwargs and not has_defaults
24962497

24972498
positional_actual_types = [
24982499
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
@@ -3768,23 +3768,31 @@ class C:
37683768
def defer() -> int:
37693769
return 1
37703770

3771-
[case testMissingPositionalArgumentShiftedTypes]
3771+
[case testMissingPositionalArgumentTypeMismatch]
37723772
def f(x: int, y: str, z: bytes, aa: int) -> None: ...
37733773

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

3779-
[case testMissingPositionalArgumentShiftedTypesFirstArg]
3779+
[case testMissingPositionalArgumentTypeMismatchFirst]
37803780
def f(x: int, y: str, z: bytes) -> None: ...
37813781

37823782
f("hello", b'x')
37833783
[builtins fixtures/primitives.pyi]
37843784
[out]
3785-
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int" (did you forget argument "x"?)
3785+
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int"
3786+
3787+
[case testMissingPositionalArgumentManyArgs]
3788+
def f(a: int, b: str, c: float, d: list[int], e: tuple[str, ...]) -> None: ...
3789+
3790+
f(1, 1.5, [1, 2, 3], ("a", "b"))
3791+
[builtins fixtures/list.pyi]
3792+
[out]
3793+
main:3: error: Argument 2 to "f" has incompatible type "float"; expected "str"
37863794

3787-
[case testMissingPositionalArgumentNoShift]
3795+
[case testMissingPositionalArgumentNoPattern]
37883796
def f(x: int, y: str, z: bytes) -> None: ...
37893797

37903798
f("wrong", 123)
@@ -3794,15 +3802,17 @@ main:3: error: Missing positional argument "z" in call to "f"
37943802
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int"
37953803
main:3: error: Argument 2 to "f" has incompatible type "int"; expected "str"
37963804

3797-
[case testMissingPositionalArgumentShiftedTypesManyArgs]
3798-
def f(a: int, b: str, c: float, d: list[int], e: tuple[str, ...]) -> None: ...
3805+
[case testMissingMultiplePositionalArguments]
3806+
def f(a: int, b: str, c: float, d: list[int]) -> None: ...
37993807

3800-
f(1, 1.5, [1, 2, 3], ("a", "b"))
3808+
f(1.5, [1, 2, 3])
38013809
[builtins fixtures/list.pyi]
38023810
[out]
3803-
main:3: error: Argument 2 to "f" has incompatible type "float"; expected "str" (did you forget argument "b"?)
3811+
main:3: error: Missing positional arguments "c", "d" in call to "f"
3812+
main:3: error: Argument 1 to "f" has incompatible type "float"; expected "int"
3813+
main:3: error: Argument 2 to "f" has incompatible type "list[int]"; expected "str"
38043814

3805-
[case testMissingPositionalArgumentShiftedWithDefaults]
3815+
[case testMissingPositionalArgumentWithDefaults]
38063816
def f(x: int, y: str, z: bytes = b'default') -> None: ...
38073817

38083818
f("hello")
@@ -3811,7 +3821,7 @@ f("hello")
38113821
main:3: error: Missing positional argument "y" in call to "f"
38123822
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int"
38133823

3814-
[case testMissingPositionalArgumentShiftedWithStarArgs]
3824+
[case testMissingPositionalArgumentWithStarArgs]
38153825
def f(x: int, y: str, z: bytes, *args: int) -> None: ...
38163826

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

0 commit comments

Comments
 (0)