@@ -2409,6 +2409,11 @@ def check_overload_call(
24092409 # typevar. See https://github.com/python/mypy/issues/4063 for related discussion.
24102410 erased_targets : list [CallableType ] | None = None
24112411 unioned_result : tuple [Type , Type ] | None = None
2412+
2413+ # Determine whether we need to encourage union math. This should be generally safe,
2414+ # as union math infers better results in the vast majority of cases, but it is very
2415+ # computationally intensive.
2416+ none_type_var_overlap = self .possible_none_type_var_overlap (arg_types , plausible_targets )
24122417 union_interrupted = False # did we try all union combinations?
24132418 if any (self .real_union (arg ) for arg in arg_types ):
24142419 try :
@@ -2421,6 +2426,7 @@ def check_overload_call(
24212426 arg_names ,
24222427 callable_name ,
24232428 object_type ,
2429+ none_type_var_overlap ,
24242430 context ,
24252431 )
24262432 except TooManyUnions :
@@ -2453,8 +2459,10 @@ def check_overload_call(
24532459 # If any of checks succeed, stop early.
24542460 if inferred_result is not None and unioned_result is not None :
24552461 # Both unioned and direct checks succeeded, choose the more precise type.
2456- if is_subtype (inferred_result [0 ], unioned_result [0 ]) and not isinstance (
2457- get_proper_type (inferred_result [0 ]), AnyType
2462+ if (
2463+ is_subtype (inferred_result [0 ], unioned_result [0 ])
2464+ and not isinstance (get_proper_type (inferred_result [0 ]), AnyType )
2465+ and not none_type_var_overlap
24582466 ):
24592467 return inferred_result
24602468 return unioned_result
@@ -2504,7 +2512,8 @@ def check_overload_call(
25042512 callable_name = callable_name ,
25052513 object_type = object_type ,
25062514 )
2507- if union_interrupted :
2515+ # Do not show the extra error if the union math was forced.
2516+ if union_interrupted and not none_type_var_overlap :
25082517 self .chk .fail (message_registry .TOO_MANY_UNION_COMBINATIONS , context )
25092518 return result
25102519
@@ -2659,6 +2668,44 @@ def overload_erased_call_targets(
26592668 matches .append (typ )
26602669 return matches
26612670
2671+ def possible_none_type_var_overlap (
2672+ self , arg_types : list [Type ], plausible_targets : list [CallableType ]
2673+ ) -> bool :
2674+ """Heuristic to determine whether we need to try forcing union math.
2675+
2676+ This is needed to avoid greedy type variable match in situations like this:
2677+ @overload
2678+ def foo(x: None) -> None: ...
2679+ @overload
2680+ def foo(x: T) -> list[T]: ...
2681+
2682+ x: int | None
2683+ foo(x)
2684+ we want this call to infer list[int] | None, not list[int | None].
2685+ """
2686+ if not plausible_targets or not arg_types :
2687+ return False
2688+ has_optional_arg = False
2689+ for arg_type in get_proper_types (arg_types ):
2690+ if not isinstance (arg_type , UnionType ):
2691+ continue
2692+ for item in get_proper_types (arg_type .items ):
2693+ if isinstance (item , NoneType ):
2694+ has_optional_arg = True
2695+ break
2696+ if not has_optional_arg :
2697+ return False
2698+
2699+ min_prefix = min (len (c .arg_types ) for c in plausible_targets )
2700+ for i in range (min_prefix ):
2701+ if any (
2702+ isinstance (get_proper_type (c .arg_types [i ]), NoneType ) for c in plausible_targets
2703+ ) and any (
2704+ isinstance (get_proper_type (c .arg_types [i ]), TypeVarType ) for c in plausible_targets
2705+ ):
2706+ return True
2707+ return False
2708+
26622709 def union_overload_result (
26632710 self ,
26642711 plausible_targets : list [CallableType ],
@@ -2668,6 +2715,7 @@ def union_overload_result(
26682715 arg_names : Sequence [str | None ] | None ,
26692716 callable_name : str | None ,
26702717 object_type : Type | None ,
2718+ none_type_var_overlap : bool ,
26712719 context : Context ,
26722720 level : int = 0 ,
26732721 ) -> list [tuple [Type , Type ]] | None :
@@ -2707,20 +2755,23 @@ def union_overload_result(
27072755
27082756 # Step 3: Try a direct match before splitting to avoid unnecessary union splits
27092757 # and save performance.
2710- with self .type_overrides_set (args , arg_types ):
2711- direct = self .infer_overload_return_type (
2712- plausible_targets ,
2713- args ,
2714- arg_types ,
2715- arg_kinds ,
2716- arg_names ,
2717- callable_name ,
2718- object_type ,
2719- context ,
2720- )
2721- if direct is not None and not isinstance (get_proper_type (direct [0 ]), (UnionType , AnyType )):
2722- # We only return non-unions soon, to avoid greedy match.
2723- return [direct ]
2758+ if not none_type_var_overlap :
2759+ with self .type_overrides_set (args , arg_types ):
2760+ direct = self .infer_overload_return_type (
2761+ plausible_targets ,
2762+ args ,
2763+ arg_types ,
2764+ arg_kinds ,
2765+ arg_names ,
2766+ callable_name ,
2767+ object_type ,
2768+ context ,
2769+ )
2770+ if direct is not None and not isinstance (
2771+ get_proper_type (direct [0 ]), (UnionType , AnyType )
2772+ ):
2773+ # We only return non-unions soon, to avoid greedy match.
2774+ return [direct ]
27242775
27252776 # Step 4: Split the first remaining union type in arguments into items and
27262777 # try to match each item individually (recursive).
@@ -2738,6 +2789,7 @@ def union_overload_result(
27382789 arg_names ,
27392790 callable_name ,
27402791 object_type ,
2792+ none_type_var_overlap ,
27412793 context ,
27422794 level + 1 ,
27432795 )
0 commit comments