@@ -2722,6 +2722,7 @@ def check_overload_call(
27222722 # for example, when we have a fallback alternative that accepts an unrestricted
27232723 # typevar. See https://github.com/python/mypy/issues/4063 for related discussion.
27242724 erased_targets : list [CallableType ] | None = None
2725+ inferred_types : list [Type ] | None = None
27252726 unioned_result : tuple [Type , Type ] | None = None
27262727
27272728 # Determine whether we need to encourage union math. This should be generally safe,
@@ -2749,13 +2750,14 @@ def check_overload_call(
27492750 # Record if we succeeded. Next we need to see if maybe normal procedure
27502751 # gives a narrower type.
27512752 if unioned_return :
2752- returns , inferred_types = zip (* unioned_return )
2753+ returns = [u [0 ] for u in unioned_return ]
2754+ inferred_types = [u [1 ] for u in unioned_return ]
27532755 # Note that we use `combine_function_signatures` instead of just returning
27542756 # a union of inferred callables because for example a call
27552757 # Union[int -> int, str -> str](Union[int, str]) is invalid and
27562758 # we don't want to introduce internal inconsistencies.
27572759 unioned_result = (
2758- make_simplified_union (list ( returns ) , context .line , context .column ),
2760+ make_simplified_union (returns , context .line , context .column ),
27592761 self .combine_function_signatures (get_proper_types (inferred_types )),
27602762 )
27612763
@@ -2770,19 +2772,26 @@ def check_overload_call(
27702772 object_type ,
27712773 context ,
27722774 )
2773- # If any of checks succeed, stop early.
2775+ # If any of checks succeed, perform deprecation tests and stop early.
27742776 if inferred_result is not None and unioned_result is not None :
27752777 # Both unioned and direct checks succeeded, choose the more precise type.
27762778 if (
27772779 is_subtype (inferred_result [0 ], unioned_result [0 ])
27782780 and not isinstance (get_proper_type (inferred_result [0 ]), AnyType )
27792781 and not none_type_var_overlap
27802782 ):
2781- return inferred_result
2782- return unioned_result
2783- elif unioned_result is not None :
2783+ unioned_result = None
2784+ else :
2785+ inferred_result = None
2786+ if unioned_result is not None :
2787+ if inferred_types is not None :
2788+ for inferred_type in inferred_types :
2789+ if isinstance (c := get_proper_type (inferred_type ), CallableType ):
2790+ self .chk .warn_deprecated (c .definition , context )
27842791 return unioned_result
2785- elif inferred_result is not None :
2792+ if inferred_result is not None :
2793+ if isinstance (c := get_proper_type (inferred_result [1 ]), CallableType ):
2794+ self .chk .warn_deprecated (c .definition , context )
27862795 return inferred_result
27872796
27882797 # Step 4: Failure. At this point, we know there is no match. We fall back to trying
@@ -2936,8 +2945,6 @@ def infer_overload_return_type(
29362945 # check for ambiguity due to 'Any' below.
29372946 if not args_contain_any :
29382947 self .chk .store_types (m )
2939- if isinstance (infer_type , ProperType ) and isinstance (infer_type , CallableType ):
2940- self .chk .warn_deprecated (infer_type .definition , context )
29412948 return ret_type , infer_type
29422949 p_infer_type = get_proper_type (infer_type )
29432950 if isinstance (p_infer_type , CallableType ):
@@ -2974,11 +2981,6 @@ def infer_overload_return_type(
29742981 else :
29752982 # Success! No ambiguity; return the first match.
29762983 self .chk .store_types (type_maps [0 ])
2977- inferred_callable = inferred_types [0 ]
2978- if isinstance (inferred_callable , ProperType ) and isinstance (
2979- inferred_callable , CallableType
2980- ):
2981- self .chk .warn_deprecated (inferred_callable .definition , context )
29822984 return return_types [0 ], inferred_types [0 ]
29832985
29842986 def overload_erased_call_targets (
0 commit comments