Skip to content

Commit 6c2bfdd

Browse files
committed
kept more consistent error messaging
1 parent 04a8121 commit 6c2bfdd

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

mypy/checkexpr.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from mypy.maptype import map_instance_to_supertype
3434
from mypy.meet import is_overlapping_types, narrow_declared_type
3535
from mypy.message_registry import ErrorMessage
36-
from mypy.messages import MessageBuilder, format_type
36+
from mypy.messages import MessageBuilder, format_type, format_type_distinctly
3737
from mypy.nodes import (
3838
ARG_NAMED,
3939
ARG_POS,
@@ -2387,12 +2387,19 @@ def check_call_arguments(
23872387
has_type_errors = type_error_watcher.has_new_errors()
23882388

23892389
if shift_info is not None:
2390-
_, param_name, expected_type, high_confidence = shift_info
2390+
shift_position, param_name, expected_type, high_confidence = shift_info
23912391
if high_confidence and param_name:
2392-
type_str = format_type(expected_type, self.chk.options)
2392+
positional_arg_types = [
2393+
arg_types[i] for i, k in enumerate(arg_kinds) if k == nodes.ARG_POS
2394+
]
2395+
actual_type = positional_arg_types[shift_position - 1]
2396+
actual_str, expected_str = format_type_distinctly(
2397+
actual_type, expected_type, options=self.chk.options
2398+
)
23932399
self.msg.fail(
2394-
f'Expected {type_str} for parameter "{param_name}"; '
2395-
f'did you forget argument "{param_name}"?',
2400+
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}"?)',
23962403
context,
23972404
code=codes.CALL_ARG,
23982405
)

test-data/unit/check-functions.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3710,15 +3710,15 @@ def f(x: int, y: str, z: bytes, aa: int) -> None: ...
37103710
f(1, b'x', 1)
37113711
[builtins fixtures/primitives.pyi]
37123712
[out]
3713-
main:3: error: Expected "str" for parameter "y"; did you forget argument "y"?
3713+
main:3: error: Argument 2 to "f" has incompatible type "bytes"; expected "str" (did you forget argument "y"?)
37143714

37153715
[case testMissingPositionalArgumentShiftedTypesFirstArg]
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: Expected "int" for parameter "x"; did you forget argument "x"?
3721+
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int" (did you forget argument "x"?)
37223722

37233723
[case testMissingPositionalArgumentNoShift]
37243724
def f(x: int, y: str, z: bytes) -> None: ...
@@ -3736,7 +3736,7 @@ def f(a: int, b: str, c: float, d: list[int], e: tuple[str, ...]) -> None: ...
37363736
f(1, 1.5, [1, 2, 3], ("a", "b"))
37373737
[builtins fixtures/list.pyi]
37383738
[out]
3739-
main:3: error: Expected "str" for parameter "b"; did you forget argument "b"?
3739+
main:3: error: Argument 2 to "f" has incompatible type "float"; expected "str" (did you forget argument "b"?)
37403740

37413741
[case testMissingPositionalArgumentShiftedWithDefaults]
37423742
def f(x: int, y: str, z: bytes = b'default') -> None: ...

0 commit comments

Comments
 (0)