@@ -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
0 commit comments