@@ -559,6 +559,11 @@ def any_constraints(options: list[list[Constraint] | None], *, eager: bool) -> l
559559 if filtered_options != options :
560560 return any_constraints (filtered_options , eager = eager )
561561
562+ # Try harder: if that didn't work, try to strip typevars not owned by current function.
563+ filtered_options = [filter_own (o ) for o in options ]
564+ if filtered_options != options :
565+ return any_constraints (filtered_options , eager = eager )
566+
562567 # Otherwise, there are either no valid options or multiple, inconsistent valid
563568 # options. Give up and deduce nothing.
564569 return []
@@ -574,12 +579,8 @@ def filter_satisfiable(option: list[Constraint] | None) -> list[Constraint] | No
574579 if not option :
575580 return option
576581
577- own = type_state .constraints_targets [- 1 ] if type_state .constraints_targets else None
578-
579582 satisfiable = []
580583 for c in option :
581- if own is not None and c .type_var not in own :
582- continue
583584 if isinstance (c .origin_type_var , TypeVarType ) and c .origin_type_var .values :
584585 if any (
585586 mypy .subtypes .is_subtype (c .target , value ) for value in c .origin_type_var .values
@@ -592,6 +593,16 @@ def filter_satisfiable(option: list[Constraint] | None) -> list[Constraint] | No
592593 return satisfiable
593594
594595
596+ def filter_own (option : list [Constraint ] | None ) -> list [Constraint ] | None :
597+ """Keep only constraints that reference type vars local to current function, if any."""
598+
599+ if not option or not type_state .constraints_targets :
600+ return option
601+ own_vars = type_state .constraints_targets [- 1 ]
602+
603+ return [c for c in option if c .type_var in own_vars ] or None
604+
605+
595606def is_same_constraints (x : list [Constraint ], y : list [Constraint ]) -> bool :
596607 for c1 in x :
597608 if not any (is_same_constraint (c1 , c2 ) for c2 in y ):
0 commit comments