Skip to content

Commit 80216cc

Browse files
committed
prevent context-less arg inference messages
I discovered this while investigating #20013. I'm not sure there's a better test case though.
1 parent 4ff1830 commit 80216cc

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

mypy/checkexpr.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,7 +2682,8 @@ def check_overload_call(
26822682
"""Checks a call to an overloaded function."""
26832683
# Normalize unpacked kwargs before checking the call.
26842684
callee = callee.with_unpacked_kwargs()
2685-
arg_types = self.infer_arg_types_in_empty_context(args)
2685+
with self.msg.filter_errors():
2686+
arg_types = self.infer_arg_types_in_empty_context(args)
26862687
# Step 1: Filter call targets to remove ones where the argument counts don't match
26872688
plausible_targets = self.plausible_overload_call_targets(
26882689
arg_types, arg_kinds, arg_names, callee
@@ -2704,7 +2705,9 @@ def check_overload_call(
27042705
union_interrupted = False # did we try all union combinations?
27052706
if any(self.real_union(arg) for arg in arg_types):
27062707
try:
2707-
with self.msg.filter_errors():
2708+
with self.msg.filter_errors(
2709+
filter_errors=True, save_filtered_errors=True
2710+
) as union_msgs:
27082711
unioned_return = self.union_overload_result(
27092712
plausible_targets,
27102713
args,
@@ -2760,6 +2763,8 @@ def check_overload_call(
27602763
for inferred_type in inferred_types:
27612764
if isinstance(c := get_proper_type(inferred_type), CallableType):
27622765
self.chk.warn_deprecated(c.definition, context)
2766+
# Use the errors the union result caused
2767+
self.msg.add_errors(union_msgs.filtered_errors())
27632768
return unioned_result
27642769
if inferred_result is not None:
27652770
if isinstance(c := get_proper_type(inferred_result[1]), CallableType):
@@ -2900,17 +2905,16 @@ def infer_overload_return_type(
29002905

29012906
for typ in plausible_targets:
29022907
assert self.msg is self.chk.msg
2903-
with self.msg.filter_errors() as w:
2904-
with self.chk.local_type_map as m:
2905-
ret_type, infer_type = self.check_call(
2906-
callee=typ,
2907-
args=args,
2908-
arg_kinds=arg_kinds,
2909-
arg_names=arg_names,
2910-
context=context,
2911-
callable_name=callable_name,
2912-
object_type=object_type,
2913-
)
2908+
with self.msg.filter_errors() as w, self.chk.local_type_map as m:
2909+
ret_type, infer_type = self.check_call(
2910+
callee=typ,
2911+
args=args,
2912+
arg_kinds=arg_kinds,
2913+
arg_names=arg_names,
2914+
context=context,
2915+
callable_name=callable_name,
2916+
object_type=object_type,
2917+
)
29142918
is_match = not w.has_new_errors()
29152919
if is_match:
29162920
# Return early if possible; otherwise record info, so we can
@@ -3078,6 +3082,9 @@ def union_overload_result(
30783082
if direct is not None and not isinstance(
30793083
get_proper_type(direct[0]), (UnionType, AnyType)
30803084
):
3085+
# Make sure arguments get messages
3086+
self.infer_arg_types_in_empty_context(args)
3087+
30813088
# We only return non-unions soon, to avoid greedy match.
30823089
return [direct]
30833090

test-data/unit/check-overloading.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6852,3 +6852,14 @@ if isinstance(headers, dict):
68526852

68536853
reveal_type(headers) # N: Revealed type is "Union[__main__.Headers, typing.Iterable[tuple[builtins.bytes, builtins.bytes]]]"
68546854
[builtins fixtures/isinstancelist.pyi]
6855+
6856+
[case testOverloadMultipleArgMessages]
6857+
from typing import overload, Literal
6858+
6859+
@overload
6860+
def foo(x: list[Literal['str']]) -> None: ...
6861+
@overload
6862+
def foo(x: int) -> None: ...
6863+
def foo(x) -> None: pass
6864+
6865+
foo(reveal_type(['str'])) # N: Revealed type is "builtins.list[Literal['str']]"

0 commit comments

Comments
 (0)