Skip to content

Commit 7da2abf

Browse files
authored
Replace TypeList in constraints with TupleType (#14257)
Now that the fallback is available, we can construct TupleTypes instead of TypeLists which will simplify constraint solving as it won't need to know to match TupleTypes with TypeLists.
1 parent dde01d6 commit 7da2abf

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

mypy/constraints.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
Type,
3030
TypeAliasType,
3131
TypedDictType,
32-
TypeList,
3332
TypeOfAny,
3433
TypeQuery,
3534
TypeType,
@@ -135,7 +134,13 @@ def infer_constraints_for_callable(
135134

136135
unpacked_type = get_proper_type(unpack_type.type)
137136
if isinstance(unpacked_type, TypeVarTupleType):
138-
constraints.append(Constraint(unpacked_type, SUPERTYPE_OF, TypeList(actual_types)))
137+
constraints.append(
138+
Constraint(
139+
unpacked_type,
140+
SUPERTYPE_OF,
141+
TupleType(actual_types, unpacked_type.tuple_fallback),
142+
)
143+
)
139144
elif isinstance(unpacked_type, TupleType):
140145
# Prefixes get converted to positional args, so technically the only case we
141146
# should have here is like Tuple[Unpack[Ts], Y1, Y2, Y3]. If this turns out
@@ -147,12 +152,13 @@ def infer_constraints_for_callable(
147152
suffix_len = len(unpacked_type.items) - 1
148153
constraints.append(
149154
Constraint(
150-
inner_unpacked_type, SUPERTYPE_OF, TypeList(actual_types[:-suffix_len])
155+
inner_unpacked_type,
156+
SUPERTYPE_OF,
157+
TupleType(actual_types[:-suffix_len], inner_unpacked_type.tuple_fallback),
151158
)
152159
)
153160
else:
154161
assert False, "mypy bug: unhandled constraint inference case"
155-
156162
else:
157163
for actual in actuals:
158164
actual_arg_type = arg_types[actual]
@@ -640,7 +646,9 @@ def visit_instance(self, template: Instance) -> list[Constraint]:
640646
if isinstance(instance_unpack, TypeVarTupleType):
641647
res.append(
642648
Constraint(
643-
instance_unpack, SUBTYPE_OF, TypeList(list(mapped_middle))
649+
instance_unpack,
650+
SUBTYPE_OF,
651+
TupleType(list(mapped_middle), instance_unpack.tuple_fallback),
644652
)
645653
)
646654
elif (
@@ -742,7 +750,9 @@ def visit_instance(self, template: Instance) -> list[Constraint]:
742750
if isinstance(template_unpack, TypeVarTupleType):
743751
res.append(
744752
Constraint(
745-
template_unpack, SUPERTYPE_OF, TypeList(list(mapped_middle))
753+
template_unpack,
754+
SUPERTYPE_OF,
755+
TupleType(list(mapped_middle), template_unpack.tuple_fallback),
746756
)
747757
)
748758
elif (

mypy/expandtype.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
Type,
2323
TypeAliasType,
2424
TypedDictType,
25-
TypeList,
2625
TypeType,
2726
TypeVarId,
2827
TypeVarLikeType,
@@ -95,7 +94,9 @@ def expand_type_by_instance(typ: Type, instance: Instance) -> Type:
9594
instance.type.type_var_tuple_prefix,
9695
instance.type.type_var_tuple_suffix,
9796
)
98-
variables = {tvars_middle[0].id: TypeList(list(args_middle))}
97+
tvar = tvars_middle[0]
98+
assert isinstance(tvar, TypeVarTupleType)
99+
variables = {tvar.id: TupleType(list(args_middle), tvar.tuple_fallback)}
99100
instance_args = args_prefix + args_suffix
100101
tvars = tvars_prefix + tvars_suffix
101102
else:
@@ -447,8 +448,6 @@ def expand_unpack_with_variables(
447448
repl = get_proper_type(variables.get(t.type.id, t))
448449
if isinstance(repl, TupleType):
449450
return repl.items
450-
if isinstance(repl, TypeList):
451-
return repl.items
452451
elif isinstance(repl, Instance) and repl.type.fullname == "builtins.tuple":
453452
return repl
454453
elif isinstance(repl, AnyType):

mypy/test/testconstraints.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from mypy.constraints import SUBTYPE_OF, SUPERTYPE_OF, Constraint, infer_constraints
66
from mypy.test.helpers import Suite
77
from mypy.test.typefixture import TypeFixture
8-
from mypy.types import Instance, TupleType, TypeList, UnpackType
8+
from mypy.types import Instance, TupleType, UnpackType
99

1010

1111
class ConstraintsSuite(Suite):
@@ -27,13 +27,19 @@ def test_basic_type_var_tuple_subtype(self) -> None:
2727
fx = self.fx
2828
assert infer_constraints(
2929
Instance(fx.gvi, [UnpackType(fx.ts)]), Instance(fx.gvi, [fx.a, fx.b]), SUBTYPE_OF
30-
) == [Constraint(type_var=fx.ts, op=SUBTYPE_OF, target=TypeList([fx.a, fx.b]))]
30+
) == [
31+
Constraint(type_var=fx.ts, op=SUBTYPE_OF, target=TupleType([fx.a, fx.b], fx.std_tuple))
32+
]
3133

3234
def test_basic_type_var_tuple(self) -> None:
3335
fx = self.fx
3436
assert infer_constraints(
3537
Instance(fx.gvi, [UnpackType(fx.ts)]), Instance(fx.gvi, [fx.a, fx.b]), SUPERTYPE_OF
36-
) == [Constraint(type_var=fx.ts, op=SUPERTYPE_OF, target=TypeList([fx.a, fx.b]))]
38+
) == [
39+
Constraint(
40+
type_var=fx.ts, op=SUPERTYPE_OF, target=TupleType([fx.a, fx.b], fx.std_tuple)
41+
)
42+
]
3743

3844
def test_type_var_tuple_with_prefix_and_suffix(self) -> None:
3945
fx = self.fx
@@ -45,7 +51,9 @@ def test_type_var_tuple_with_prefix_and_suffix(self) -> None:
4551
)
4652
) == {
4753
Constraint(type_var=fx.t, op=SUPERTYPE_OF, target=fx.a),
48-
Constraint(type_var=fx.ts, op=SUPERTYPE_OF, target=TypeList([fx.b, fx.c])),
54+
Constraint(
55+
type_var=fx.ts, op=SUPERTYPE_OF, target=TupleType([fx.b, fx.c], fx.std_tuple)
56+
),
4957
Constraint(type_var=fx.s, op=SUPERTYPE_OF, target=fx.d),
5058
}
5159

0 commit comments

Comments
 (0)