Skip to content

Commit 5e0d653

Browse files
committed
made error messages more consistent and improved test names to not include implementation details
1 parent a22ae14 commit 5e0d653

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
@@ -2399,8 +2399,7 @@ def check_call_arguments(
23992399
)
24002400
self.msg.fail(
24012401
f'Argument {shift_position} to "{func_name}" has incompatible type '
2402-
f"{actual_str}; expected {expected_str} "
2403-
f'(did you forget argument "{param_name}"?)',
2402+
f"{actual_str}; expected {expected_str}",
24042403
context,
24052404
code=codes.CALL_ARG,
24062405
)
@@ -2492,13 +2491,15 @@ def detect_shifted_positional_args(
24922491
if not missing_positional:
24932492
return None
24942493

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

25032504
positional_actual_types = [
25042505
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
@@ -3751,23 +3751,31 @@ kwargs: dict[str, object]
37513751
foo(**kwargs) # E: Argument 1 to "foo" has incompatible type "**dict[str, object]"; expected "P"
37523752
[builtins fixtures/dict.pyi]
37533753

3754-
[case testMissingPositionalArgumentShiftedTypes]
3754+
[case testMissingPositionalArgumentTypeMismatch]
37553755
def f(x: int, y: str, z: bytes, aa: int) -> None: ...
37563756

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

3762-
[case testMissingPositionalArgumentShiftedTypesFirstArg]
3762+
[case testMissingPositionalArgumentTypeMismatchFirst]
37633763
def f(x: int, y: str, z: bytes) -> None: ...
37643764

37653765
f("hello", b'x')
37663766
[builtins fixtures/primitives.pyi]
37673767
[out]
3768-
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int" (did you forget argument "x"?)
3768+
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int"
3769+
3770+
[case testMissingPositionalArgumentManyArgs]
3771+
def f(a: int, b: str, c: float, d: list[int], e: tuple[str, ...]) -> None: ...
3772+
3773+
f(1, 1.5, [1, 2, 3], ("a", "b"))
3774+
[builtins fixtures/list.pyi]
3775+
[out]
3776+
main:3: error: Argument 2 to "f" has incompatible type "float"; expected "str"
37693777

3770-
[case testMissingPositionalArgumentNoShift]
3778+
[case testMissingPositionalArgumentNoPattern]
37713779
def f(x: int, y: str, z: bytes) -> None: ...
37723780

37733781
f("wrong", 123)
@@ -3777,15 +3785,17 @@ main:3: error: Missing positional argument "z" in call to "f"
37773785
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int"
37783786
main:3: error: Argument 2 to "f" has incompatible type "int"; expected "str"
37793787

3780-
[case testMissingPositionalArgumentShiftedTypesManyArgs]
3781-
def f(a: int, b: str, c: float, d: list[int], e: tuple[str, ...]) -> None: ...
3788+
[case testMissingMultiplePositionalArguments]
3789+
def f(a: int, b: str, c: float, d: list[int]) -> None: ...
37823790

3783-
f(1, 1.5, [1, 2, 3], ("a", "b"))
3791+
f(1.5, [1, 2, 3])
37843792
[builtins fixtures/list.pyi]
37853793
[out]
3786-
main:3: error: Argument 2 to "f" has incompatible type "float"; expected "str" (did you forget argument "b"?)
3794+
main:3: error: Missing positional arguments "c", "d" in call to "f"
3795+
main:3: error: Argument 1 to "f" has incompatible type "float"; expected "int"
3796+
main:3: error: Argument 2 to "f" has incompatible type "list[int]"; expected "str"
37873797

3788-
[case testMissingPositionalArgumentShiftedWithDefaults]
3798+
[case testMissingPositionalArgumentWithDefaults]
37893799
def f(x: int, y: str, z: bytes = b'default') -> None: ...
37903800

37913801
f("hello")
@@ -3794,7 +3804,7 @@ f("hello")
37943804
main:3: error: Missing positional argument "y" in call to "f"
37953805
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int"
37963806

3797-
[case testMissingPositionalArgumentShiftedWithStarArgs]
3807+
[case testMissingPositionalArgumentWithStarArgs]
37983808
def f(x: int, y: str, z: bytes, *args: int) -> None: ...
37993809

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

0 commit comments

Comments
 (0)