@@ -512,7 +512,7 @@ def handle_recursive_union(template: UnionType, actual: Type, direction: int) ->
512512 ) or infer_constraints (UnionType .make_union (type_var_items ), actual , direction )
513513
514514
515- def any_constraints (options : list [list [Constraint ] | None ], eager : bool ) -> list [Constraint ]:
515+ def any_constraints (options : list [list [Constraint ] | None ], * , eager : bool ) -> list [Constraint ]:
516516 """Deduce what we can from a collection of constraint lists.
517517
518518 It's a given that at least one of the lists must be satisfied. A
@@ -547,14 +547,22 @@ def any_constraints(options: list[list[Constraint] | None], eager: bool) -> list
547547 if option in trivial_options :
548548 continue
549549 merged_options .append ([merge_with_any (c ) for c in option ])
550- return any_constraints (list (merged_options ), eager )
550+ return any_constraints (list (merged_options ), eager = eager )
551551
552552 # If normal logic didn't work, try excluding trivially unsatisfiable constraint (due to
553553 # upper bounds) from each option, and comparing them again.
554554 filtered_options = [filter_satisfiable (o ) for o in options ]
555555 if filtered_options != options :
556556 return any_constraints (filtered_options , eager = eager )
557557
558+ # Try harder: if that didn't work, try to strip typevars that aren't meta vars.
559+ # Note this is what we would always do, but unfortunately some callers may not
560+ # set the meta var status correctly (for historical reasons), so we use this as
561+ # a fallback only.
562+ filtered_options = [exclude_non_meta_vars (o ) for o in options ]
563+ if filtered_options != options :
564+ return any_constraints (filtered_options , eager = eager )
565+
558566 # Otherwise, there are either no valid options or multiple, inconsistent valid
559567 # options. Give up and deduce nothing.
560568 return []
@@ -569,6 +577,7 @@ def filter_satisfiable(option: list[Constraint] | None) -> list[Constraint] | No
569577 """
570578 if not option :
571579 return option
580+
572581 satisfiable = []
573582 for c in option :
574583 if isinstance (c .origin_type_var , TypeVarType ) and c .origin_type_var .values :
@@ -583,6 +592,15 @@ def filter_satisfiable(option: list[Constraint] | None) -> list[Constraint] | No
583592 return satisfiable
584593
585594
595+ def exclude_non_meta_vars (option : list [Constraint ] | None ) -> list [Constraint ] | None :
596+ # If we had an empty list, keep it intact
597+ if not option :
598+ return option
599+ # However, if none of the options actually references meta vars, better remove
600+ # this constraint entirely.
601+ return [c for c in option if c .type_var .is_meta_var ()] or None
602+
603+
586604def is_same_constraints (x : list [Constraint ], y : list [Constraint ]) -> bool :
587605 for c1 in x :
588606 if not any (is_same_constraint (c1 , c2 ) for c2 in y ):
0 commit comments