@@ -16,25 +16,6 @@ def map_instance_to_supertype(instance: Instance, superclass: TypeInfo) -> Insta
1616 # Fast path: `instance` already belongs to `superclass`.
1717 return instance
1818
19- if superclass .fullname == "builtins.tuple" and instance .type .tuple_type :
20- if has_type_vars (instance .type .tuple_type ):
21- # We special case mapping generic tuple types to tuple base, because for
22- # such tuples fallback can't be calculated before applying type arguments.
23- alias = instance .type .special_alias
24- assert alias is not None
25- if not alias ._is_recursive :
26- # Unfortunately we can't support this for generic recursive tuples.
27- # If we skip this special casing we will fall back to tuple[Any, ...].
28- tuple_type = expand_type_by_instance (instance .type .tuple_type , instance )
29- if isinstance (tuple_type , TupleType ):
30- # Make the import here to avoid cyclic imports.
31- import mypy .typeops
32-
33- return mypy .typeops .tuple_fallback (tuple_type )
34- elif isinstance (tuple_type , Instance ):
35- # This can happen after normalizing variadic tuples.
36- return tuple_type
37-
3819 if not superclass .type_vars :
3920 # Fast path: `superclass` has no type variables to map to.
4021 return Instance (superclass , [])
@@ -93,6 +74,28 @@ def map_instance_to_direct_supertypes(instance: Instance, supertype: TypeInfo) -
9374
9475 for b in typ .bases :
9576 if b .type == supertype :
77+
78+ if supertype .fullname == "builtins.tuple" and instance .type .tuple_type :
79+ if has_type_vars (instance .type .tuple_type ):
80+ # We special case mapping generic tuple types to tuple base, because for
81+ # such tuples fallback can't be calculated before applying type arguments.
82+ alias = instance .type .special_alias
83+ assert alias is not None
84+ if not alias ._is_recursive :
85+ # Unfortunately we can't support this for generic recursive tuples.
86+ # If we skip this special casing we will fall back to tuple[Any, ...].
87+ tuple_type = expand_type_by_instance (instance .type .tuple_type , instance )
88+ if isinstance (tuple_type , TupleType ):
89+ # Make the import here to avoid cyclic imports.
90+ import mypy .typeops
91+
92+ result .append (mypy .typeops .tuple_fallback (tuple_type ))
93+ continue
94+ elif isinstance (tuple_type , Instance ):
95+ # This can happen after normalizing variadic tuples.
96+ result .append (tuple_type )
97+ continue
98+
9699 t = expand_type_by_instance (b , instance )
97100 assert isinstance (t , Instance )
98101 result .append (t )
0 commit comments