Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2813,7 +2813,9 @@ def check_overload_call(
code = None
else:
code = codes.OPERATOR
self.msg.no_variant_matches_arguments(callee, arg_types, context, code=code)
self.msg.no_variant_matches_arguments(
callee, arg_types, context, arg_names=arg_names, arg_kinds=arg_kinds, code=code
)

result = self.check_call(
target,
Expand Down
76 changes: 76 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
ArgKind,
CallExpr,
Context,
Decorator,
Expression,
FuncDef,
IndexExpr,
Expand Down Expand Up @@ -1085,14 +1086,89 @@ def no_variant_matches_arguments(
arg_types: list[Type],
context: Context,
*,
arg_names: Sequence[str | None] | None,
arg_kinds: list[ArgKind] | None = None,
code: ErrorCode | None = None,
) -> None:
code = code or codes.CALL_OVERLOAD
name = callable_name(overload)
if name:
name_str = f" of {name}"
for_func = f" for overloaded function {name}"
else:
name_str = ""
for_func = ""

# For keyword argument errors
unexpected_kwargs: list[tuple[str, Type]] = []
if arg_names is not None and arg_kinds is not None:
all_valid_kwargs: set[str] = set()
for item in overload.items:
for i, arg_name in enumerate(item.arg_names):
if arg_name is not None and item.arg_kinds[i] != ARG_STAR:
all_valid_kwargs.add(arg_name)
if item.is_kw_arg:
all_valid_kwargs.clear()
break

if all_valid_kwargs:
for i, (arg_name, arg_kind) in enumerate(zip(arg_names, arg_kinds)):
if arg_kind == ARG_NAMED and arg_name is not None:
if arg_name not in all_valid_kwargs:
unexpected_kwargs.append((arg_name, arg_types[i]))

if unexpected_kwargs:
for kwarg_name, kwarg_type in unexpected_kwargs:
matching_type_args: list[str] = []
not_matching_type_args: list[str] = []
matching_variant: CallableType | None = None

for item in overload.items:
has_type_match = False
for i, formal_type in enumerate(item.arg_types):
formal_name = item.arg_names[i]
if formal_name is not None and item.arg_kinds[i] != ARG_STAR:
if is_subtype(kwarg_type, formal_type):
if formal_name not in matching_type_args:
matching_type_args.append(formal_name)
has_type_match = True
else:
if formal_name not in not_matching_type_args:
not_matching_type_args.append(formal_name)
if has_type_match and matching_variant is None:
matching_variant = item

matches = best_matches(kwarg_name, matching_type_args, n=3)
if not matches:
matches = best_matches(kwarg_name, not_matching_type_args, n=3)

msg = f'Unexpected keyword argument "{kwarg_name}"' + for_func

if matching_variant is not None and matching_variant.definition is not None:
defn = matching_variant.definition
if isinstance(defn, Decorator):
func_line = defn.func.line
else:
func_line = defn.line
msg += f" defined on line {func_line}"

if matches:
msg += f"; did you mean {pretty_seq(matches, 'or')}?"
self.fail(msg, context, code=code)

if matching_variant is None:
self.note(
f"Possible overload variant{plural_s(len(overload.items))}:",
context,
code=code,
)
for item in overload.items:
self.note(
pretty_callable(item, self.options), context, offset=4, code=code
)

return

arg_types_str = ", ".join(format_type(arg, self.options) for arg in arg_types)
num_args = len(arg_types)
if num_args == 0:
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2561,3 +2561,21 @@ def last_known_value() -> None:
x, y, z = xy # E: Unpacking a string is disallowed
reveal_type(z) # N: Revealed type is "builtins.str"
[builtins fixtures/primitives.pyi]

[case testInvalidArgumentInOverloadError]
from typing import overload, Union

@overload
def f(foobar: int) -> None: ...

@overload
def f(foobar: str) -> None: ...

def f(foobar: Union[int, str]) -> None:
pass

f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f" defined on line 4; did you mean "foobar"?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't report the line number here. It could be useful to report it, but we generally use a note, since the function could be in a different file so line number by itself isn't sufficient.

f(random=[1,2,3]) # E: Unexpected keyword argument "random" for overloaded function "f" \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional test ideas:

  • Test multiple invalid keyword arguments
  • Test both invalid keyword argument and incompatible positional argument
  • Test both valid an invalid keyword arguments in the same call

# N: Possible overload variants: \
# N: def f(foobar: int) -> None \
# N: def f(foobar: str) -> None