diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 9990caaeb7a1..1ac66aa57654 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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, diff --git a/mypy/messages.py b/mypy/messages.py index 1e589e1bdf04..a300d7c0446c 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -1085,14 +1085,81 @@ 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 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: diff --git a/mypy/semanal.py b/mypy/semanal.py index 11f0156372bf..20bcb2f4ac60 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -493,10 +493,6 @@ def __init__( # since it's possible that the name will be there once the namespace is complete. self.incomplete_namespaces = incomplete_namespaces self.all_exports: list[str] = [] - # Map from module id to list of explicitly exported names (i.e. names in __all__). - # This is used by stubgen/stubtest, DO NOT use for any other purposes as it is - # not populated on incremental runs (nor in parallel mode). - self.export_map: dict[str, list[str]] = {} self.plugin = plugin # If True, process function definitions. If False, don't. This is used # for processing module top levels in fine-grained incremental mode. @@ -724,7 +720,6 @@ def refresh_top_level(self, file_node: MypyFile) -> None: if file_node.fullname == "typing_extensions": self.add_typing_extension_aliases(file_node) self.adjust_public_exports() - self.export_map[self.cur_mod_id] = self.all_exports self.all_exports = [] def add_implicit_module_attrs(self, file_node: MypyFile) -> None: @@ -4061,7 +4056,11 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool: type_params: TypeVarLikeList | None all_type_params_names: list[str] | None if self.check_type_alias_type_call(s.rvalue, name=lvalue.name): - rvalue = s.rvalue.args[1] + rvalue = ( + s.rvalue.args[1] + if s.rvalue.arg_kinds[1] == ARG_POS + else s.rvalue.args[s.rvalue.arg_names.index("value")] + ) pep_695 = True type_params, all_type_params_names = self.analyze_type_alias_type_params(s.rvalue) else: @@ -4249,7 +4248,9 @@ def check_type_alias_type_call(self, rvalue: Expression, *, name: str) -> TypeGu return False if not self.check_typevarlike_name(rvalue, name, rvalue): return False - if rvalue.arg_kinds.count(ARG_POS) != 2: + if rvalue.arg_kinds.count(ARG_POS) != ( + 2 - ("value" in rvalue.arg_names) - ("name" in rvalue.arg_names) + ): return False return True diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test index 30b1f1a68e15..2bbbd7a3992d 100644 --- a/test-data/unit/check-expressions.test +++ b/test-data/unit/check-expressions.test @@ -2561,3 +2561,38 @@ 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"; did you mean "foobar"? +f(random=[1,2,3]) # E: Unexpected keyword argument "random" for overloaded function "f" \ + # N: Possible overload variants: \ + # N: def f(foobar: int) -> None \ + # N: def f(foobar: str) -> None + +f(fobar=1, baz=2) # E: Unexpected keyword argument "fobar" for overloaded function "f"; did you mean "foobar"? \ + # E: Unexpected keyword argument "baz" for overloaded function "f" + +f(foobar=1, invalid=2) # E: Unexpected keyword argument "invalid" for overloaded function "f" + +@overload +def g(x: int, y: int) -> int: ... + +@overload +def g(x: str, y: str) -> str: ... + +def g(x: Union[int, str], y: Union[int, str]) -> Union[int, str]: + return x + +g([1, 2], z=3) # E: Unexpected keyword argument "z" for overloaded function "g" +[builtins fixtures/list.pyi]