@@ -565,12 +565,24 @@ def any_constraints(options: list[list[Constraint] | None], *, eager: bool) -> l
565565 and not any (isinstance (c .target , ErasedType ) for group in valid_options for c in group )
566566 ):
567567 # Now we know all constraints might be satisfiable and have similar structure.
568- # Solver will apply meets and joins as necessary, return everything we know.
569- # Just deduplicate to reduce the amount of work .
568+ # Solver will apply meets and joins as necessary, but Any should be forced into
569+ # union to survive during meet .
570570 # If any targets are erased, fall back to empty, otherwise they will be discarded
571571 # by solver, causing false early matches.
572- all_combined = sum (valid_options , [])
573- return list (dict .fromkeys (all_combined ))
572+ cmap : dict [TypeVarId , list [Constraint ]] = {}
573+ for option in valid_options :
574+ for c in option :
575+ cmap .setdefault (c .type_var , []).append (c )
576+ out : list [Constraint ] = []
577+ for group in cmap .values ():
578+ if any (isinstance (get_proper_type (c .target ), AnyType ) for c in group ):
579+ group = [
580+ merge_with_any (c )
581+ for c in group
582+ if not isinstance (get_proper_type (c .target ), AnyType )
583+ ]
584+ out .extend (dict .fromkeys (group ))
585+ return out
574586
575587 # Otherwise, there are either no valid options or multiple, inconsistent valid
576588 # options. Give up and deduce nothing.
0 commit comments