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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2682,7 +2682,8 @@ def check_overload_call(
"""Checks a call to an overloaded function."""
# Normalize unpacked kwargs before checking the call.
callee = callee.with_unpacked_kwargs()
arg_types = self.infer_arg_types_in_empty_context(args)
with self.msg.filter_errors():
arg_types = self.infer_arg_types_in_empty_context(args)
# Step 1: Filter call targets to remove ones where the argument counts don't match
plausible_targets = self.plausible_overload_call_targets(
arg_types, arg_kinds, arg_names, callee
Expand All @@ -2704,7 +2705,9 @@ def check_overload_call(
union_interrupted = False # did we try all union combinations?
if any(self.real_union(arg) for arg in arg_types):
try:
with self.msg.filter_errors():
with self.msg.filter_errors(
filter_errors=True, save_filtered_errors=True
) as union_msgs:
unioned_return = self.union_overload_result(
plausible_targets,
args,
Expand Down Expand Up @@ -2760,6 +2763,8 @@ def check_overload_call(
for inferred_type in inferred_types:
if isinstance(c := get_proper_type(inferred_type), CallableType):
self.chk.warn_deprecated(c.definition, context)
# Use the errors the union result caused
self.msg.add_errors(union_msgs.filtered_errors())
return unioned_result
if inferred_result is not None:
if isinstance(c := get_proper_type(inferred_result[1]), CallableType):
Expand Down Expand Up @@ -2900,17 +2905,16 @@ def infer_overload_return_type(

for typ in plausible_targets:
assert self.msg is self.chk.msg
with self.msg.filter_errors() as w:
with self.chk.local_type_map as m:
ret_type, infer_type = self.check_call(
callee=typ,
args=args,
arg_kinds=arg_kinds,
arg_names=arg_names,
context=context,
callable_name=callable_name,
object_type=object_type,
)
with self.msg.filter_errors() as w, self.chk.local_type_map as m:
ret_type, infer_type = self.check_call(
callee=typ,
args=args,
arg_kinds=arg_kinds,
arg_names=arg_names,
context=context,
callable_name=callable_name,
object_type=object_type,
)
is_match = not w.has_new_errors()
if is_match:
# Return early if possible; otherwise record info, so we can
Expand Down Expand Up @@ -3058,6 +3062,9 @@ def union_overload_result(
context,
)
if res is not None:
# Make sure arguments get messages
self.infer_arg_types_in_empty_context(args)

return [res]
return None

Expand All @@ -3078,6 +3085,9 @@ def union_overload_result(
if direct is not None and not isinstance(
get_proper_type(direct[0]), (UnionType, AnyType)
):
# Make sure arguments get messages
self.infer_arg_types_in_empty_context(args)

# We only return non-unions soon, to avoid greedy match.
return [direct]

Expand Down
28 changes: 28 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -6852,3 +6852,31 @@ if isinstance(headers, dict):

reveal_type(headers) # N: Revealed type is "Union[__main__.Headers, typing.Iterable[tuple[builtins.bytes, builtins.bytes]]]"
[builtins fixtures/isinstancelist.pyi]

[case testOverloadMultipleArgMessages]
from typing import overload, Literal

@overload
def foo(x: list[Literal['str']]) -> None: ...
@overload
def foo(x: int) -> None: ...
def foo(x) -> None: pass

foo(reveal_type(['str'])) # N: Revealed type is "builtins.list[Literal['str']]"

[case testOverloadUnionArgumentsEvaluated]
from typing import overload, Optional

@overload
def blah(k: str, o: None = None) -> Optional[str]: ...

@overload
def blah(k: str, o: int) -> str: ...

def blah(k, o=None) -> Optional[str]: ...

class X:
m: str

x: Optional[X] = None
blah(x.m) # E: Item "None" of "Optional[X]" has no attribute "m"