Skip to content

Commit 61b0807

Browse files
committed
added line number to error message
1 parent 746df04 commit 61b0807

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

mypy/messages.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
ArgKind,
4545
CallExpr,
4646
Context,
47+
Decorator,
4748
Expression,
4849
FuncDef,
4950
IndexExpr,
@@ -1104,7 +1105,7 @@ def no_variant_matches_arguments(
11041105
all_valid_kwargs: set[str] = set()
11051106
for item in overload.items:
11061107
for i, arg_name in enumerate(item.arg_names):
1107-
if arg_name is not None and arg_kinds[i] != ARG_STAR:
1108+
if arg_name is not None and item.arg_kinds[i] != ARG_STAR:
11081109
all_valid_kwargs.add(arg_name)
11091110
if item.is_kw_arg:
11101111
all_valid_kwargs.clear()
@@ -1120,28 +1121,41 @@ def no_variant_matches_arguments(
11201121
for kwarg_name, kwarg_type in unexpected_kwargs:
11211122
matching_type_args: list[str] = []
11221123
not_matching_type_args: list[str] = []
1124+
matching_variant: CallableType | None = None
11231125

11241126
for item in overload.items:
1127+
has_type_match = False
11251128
for i, formal_type in enumerate(item.arg_types):
11261129
formal_name = item.arg_names[i]
11271130
if formal_name is not None and item.arg_kinds[i] != ARG_STAR:
11281131
if is_subtype(kwarg_type, formal_type):
11291132
if formal_name not in matching_type_args:
11301133
matching_type_args.append(formal_name)
1134+
has_type_match = True
11311135
else:
11321136
if formal_name not in not_matching_type_args:
11331137
not_matching_type_args.append(formal_name)
1138+
if has_type_match and matching_variant is None:
1139+
matching_variant = item
11341140

11351141
matches = best_matches(kwarg_name, matching_type_args, n=3)
11361142
if not matches:
11371143
matches = best_matches(kwarg_name, not_matching_type_args, n=3)
11381144

11391145
msg = f'Unexpected keyword argument "{kwarg_name}"' + for_func
1146+
1147+
if matching_variant is not None and matching_variant.definition is not None:
1148+
defn = matching_variant.definition
1149+
if isinstance(defn, Decorator):
1150+
func_line = defn.func.line
1151+
else:
1152+
func_line = defn.line
1153+
msg += f" defined on line {func_line}"
1154+
11401155
if matches:
11411156
msg += f"; did you mean {pretty_seq(matches, 'or')}?"
11421157
self.fail(msg, context, code=code)
11431158

1144-
# do we want to still show possible overload variants as a note?
11451159
return
11461160

11471161
arg_types_str = ", ".join(format_type(arg, self.options) for arg in arg_types)

test-data/unit/check-expressions.test

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,17 +2563,15 @@ def last_known_value() -> None:
25632563
[builtins fixtures/primitives.pyi]
25642564

25652565
[case testInvalidArgumentInOverloadError]
2566-
from typing import overload
2566+
from typing import overload, Union
25672567

2568-
@overload
2569-
def f(foobar: int) -> None:
2570-
pass
2568+
@overload
2569+
def f(foobar: int) -> None: ...
25712570

25722571
@overload
2573-
def f(foobar: str) -> None:
2574-
pass
2572+
def f(foobar: str) -> None: ...
25752573

2576-
def f(foobar: bool) -> None:
2574+
def f(foobar: Union[int, str]) -> None:
25772575
pass
25782576

2579-
f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f"; did you mean "foobar"?
2577+
f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f" defined on line 4; did you mean "foobar"?

0 commit comments

Comments
 (0)