Skip to content

Commit c119fc8

Browse files
committed
Do it the crude way to see real implications
1 parent 6ff817f commit c119fc8

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

mypy/checkexpr.py

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,33 +2892,31 @@ def infer_overload_return_type(
28922892
Assumes all of the given targets have argument counts compatible with the caller.
28932893
"""
28942894

2895-
matches: list[CallableType] = []
2896-
return_types: list[Type] = []
2897-
inferred_types: list[Type] = []
28982895
args_contain_any = any(map(has_any_type, arg_types))
2899-
type_maps: list[dict[Expression, Type]] = []
29002896

2897+
# First do a pass without external context and find all overloads that
2898+
# can be possibly matched. If no Any is present among args, bail out early
2899+
# on the first match.
2900+
candidates = []
29012901
for typ in plausible_targets:
29022902
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-
# Overload selection should not depend on the context.
2906-
# During this step pretend that we do not have any external information.
2907-
self.type_context.append(None)
2908-
ret_type, infer_type = self.check_call(
2909-
callee=typ,
2910-
args=args,
2911-
arg_kinds=arg_kinds,
2912-
arg_names=arg_names,
2913-
context=context,
2914-
callable_name=callable_name,
2915-
object_type=object_type,
2916-
)
2917-
self.type_context.pop()
2903+
with self.msg.filter_errors() as w, self.chk.local_type_map as m:
2904+
# Overload selection should not depend on the context.
2905+
# During this step pretend that we do not have any external information.
2906+
self.type_context.append(None)
2907+
ret_type, infer_type = self.check_call(
2908+
callee=typ,
2909+
args=args,
2910+
arg_kinds=arg_kinds,
2911+
arg_names=arg_names,
2912+
context=context,
2913+
callable_name=callable_name,
2914+
object_type=object_type,
2915+
)
2916+
self.type_context.pop()
29182917
is_match = not w.has_new_errors()
29192918
if is_match:
2920-
# Return early if possible; otherwise record info, so we can
2921-
# check for ambiguity due to 'Any' below.
2919+
# Return early if possible
29222920
if not args_contain_any:
29232921
# Yes, just again
29242922
# FIXME: find a way to avoid doing this
@@ -2931,6 +2929,31 @@ def infer_overload_return_type(
29312929
callable_name=callable_name,
29322930
object_type=object_type,
29332931
)
2932+
candidates.append(typ)
2933+
2934+
# Repeat the same with outer context, but only for the select candidates.
2935+
matches: list[CallableType] = []
2936+
return_types: list[Type] = []
2937+
inferred_types: list[Type] = []
2938+
type_maps: list[dict[Expression, Type]] = []
2939+
2940+
for typ in candidates:
2941+
assert self.msg is self.chk.msg
2942+
with self.msg.filter_errors() as w, self.chk.local_type_map as m:
2943+
# Overload selection should not depend on the context.
2944+
# During this step pretend that we do not have any external information.
2945+
ret_type, infer_type = self.check_call(
2946+
callee=typ,
2947+
args=args,
2948+
arg_kinds=arg_kinds,
2949+
arg_names=arg_names,
2950+
context=context,
2951+
callable_name=callable_name,
2952+
object_type=object_type,
2953+
)
2954+
is_match = not w.has_new_errors()
2955+
if is_match:
2956+
# Record info, so we can check for ambiguity due to 'Any' below.
29342957
p_infer_type = get_proper_type(infer_type)
29352958
if isinstance(p_infer_type, CallableType):
29362959
# Prefer inferred types if possible, this will avoid false triggers for

test-data/unit/check-overloading.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6865,5 +6865,5 @@ def gather(*fns: object) -> int: ...
68656865

68666866
def crash() -> None:
68676867
foo: str
6868-
(foo,) = gather(0) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
6868+
(foo,) = gather(0) # E: Argument 1 to "gather" has incompatible type "int"; expected "str"
68696869
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)