@@ -2876,69 +2876,55 @@ def infer_overload_return_type(
28762876
28772877 Assumes all of the given targets have argument counts compatible with the caller.
28782878 """
2879-
28802879 matches : list [CallableType ] = []
28812880 return_types : list [Type ] = []
28822881 inferred_types : list [Type ] = []
2883- args_contain_any = any (map (has_any_type , arg_types ))
28842882 type_maps : list [dict [Expression , Type ]] = []
2883+ args_contain_any = any (map (has_any_type , arg_types ))
28852884
28862885 for typ in plausible_targets :
28872886 assert self .msg is self .chk .msg
2888- with self .msg .filter_errors () as w :
2889- with self .chk .local_type_map () as m :
2890- ret_type , infer_type = self .check_call (
2891- callee = typ ,
2892- args = args ,
2893- arg_kinds = arg_kinds ,
2894- arg_names = arg_names ,
2895- context = context ,
2896- callable_name = callable_name ,
2897- object_type = object_type ,
2898- )
2899- is_match = not w .has_new_errors ()
2900- if is_match :
2901- # Return early if possible; otherwise record info, so we can
2902- # check for ambiguity due to 'Any' below.
2903- if not args_contain_any :
2904- self .chk .store_types (m )
2905- return ret_type , infer_type
2906- p_infer_type = get_proper_type (infer_type )
2907- if isinstance (p_infer_type , CallableType ):
2908- # Prefer inferred types if possible, this will avoid false triggers for
2909- # Any-ambiguity caused by arguments with Any passed to generic overloads.
2910- matches .append (p_infer_type )
2911- else :
2912- matches .append (typ )
2913- return_types .append (ret_type )
2914- inferred_types .append (infer_type )
2915- type_maps .append (m )
2887+ with self .msg .filter_errors () as w , self .chk .local_type_map () as m :
2888+ ret_type , infer_type = self .check_call (
2889+ callee = typ , args = args , arg_kinds = arg_kinds , arg_names = arg_names ,
2890+ context = context , callable_name = callable_name , object_type = object_type )
2891+ if w .has_new_errors (): continue
2892+
2893+ # Return early if possible; otherwise record info, so we can
2894+ # check for ambiguity due to 'Any' below.
2895+ if not args_contain_any :
2896+ self .chk .store_types (m )
2897+ return ret_type , infer_type
2898+
2899+ # Prefer inferred types if possible, this will avoid false triggers for
2900+ # Any-ambiguity caused by arguments with Any passed to generic overloads.
2901+ p = get_proper_type (infer_type )
2902+ matches .append (p if isinstance (p , CallableType ) else typ )
2903+ return_types .append (ret_type )
2904+ inferred_types .append (infer_type )
2905+ type_maps .append (m )
29162906
29172907 if not matches :
29182908 return None
2919- elif any_causes_overload_ambiguity (matches , return_types , arg_types , arg_kinds , arg_names ):
2920- # An argument of type or containing the type 'Any' caused ambiguity.
2921- # We try returning a precise type if we can. If not, we give up and just return 'Any'.
2909+
2910+ # An argument of type or containing the type 'Any' caused ambiguity.
2911+ # We try returning a precise type if we can. If not, we give up and just return 'Any'.
2912+ if any_causes_overload_ambiguity (matches , return_types , arg_types , arg_kinds , arg_names ):
29222913 if all_same_types (return_types ):
29232914 self .chk .store_types (type_maps [0 ])
29242915 return return_types [0 ], inferred_types [0 ]
2925- elif all_same_types ([erase_type (typ ) for typ in return_types ]):
2916+ erased = [erase_type (t ) for t in return_types ]
2917+ if all_same_types (cast (list [Type ], erased )):
29262918 self .chk .store_types (type_maps [0 ])
29272919 return erase_type (return_types [0 ]), erase_type (inferred_types [0 ])
2928- else :
2929- return self .check_call (
2930- callee = AnyType (TypeOfAny .special_form ),
2931- args = args ,
2932- arg_kinds = arg_kinds ,
2933- arg_names = arg_names ,
2934- context = context ,
2935- callable_name = callable_name ,
2936- object_type = object_type ,
2937- )
2938- else :
2939- # Success! No ambiguity; return the first match.
2940- self .chk .store_types (type_maps [0 ])
2941- return return_types [0 ], inferred_types [0 ]
2920+ return self .check_call (
2921+ callee = AnyType (TypeOfAny .special_form ), args = args , arg_kinds = arg_kinds ,
2922+ arg_names = arg_names , context = context , callable_name = callable_name ,
2923+ object_type = object_type )
2924+
2925+ # Success! No ambiguity; return the first match.
2926+ self .chk .store_types (type_maps [0 ])
2927+ return return_types [0 ], inferred_types [0 ]
29422928
29432929 def overload_erased_call_targets (
29442930 self ,
0 commit comments