|
5 | 5 | from mypy.constraints import SUBTYPE_OF, SUPERTYPE_OF, Constraint, infer_constraints
|
6 | 6 | from mypy.test.helpers import Suite
|
7 | 7 | from mypy.test.typefixture import TypeFixture
|
8 |
| -from mypy.types import Instance, TypeList, UnpackType |
| 8 | +from mypy.types import Instance, TupleType, TypeList, UnpackType |
9 | 9 |
|
10 | 10 |
|
11 | 11 | class ConstraintsSuite(Suite):
|
@@ -48,3 +48,98 @@ def test_type_var_tuple_with_prefix_and_suffix(self) -> None:
|
48 | 48 | Constraint(type_var=fx.ts, op=SUPERTYPE_OF, target=TypeList([fx.b, fx.c])),
|
49 | 49 | Constraint(type_var=fx.s, op=SUPERTYPE_OF, target=fx.d),
|
50 | 50 | }
|
| 51 | + |
| 52 | + def test_unpack_homogenous_tuple(self) -> None: |
| 53 | + fx = self.fx |
| 54 | + assert set( |
| 55 | + infer_constraints( |
| 56 | + Instance(fx.gvi, [UnpackType(Instance(fx.std_tuplei, [fx.t]))]), |
| 57 | + Instance(fx.gvi, [fx.a, fx.b]), |
| 58 | + SUPERTYPE_OF, |
| 59 | + ) |
| 60 | + ) == { |
| 61 | + Constraint(type_var=fx.t, op=SUPERTYPE_OF, target=fx.a), |
| 62 | + Constraint(type_var=fx.t, op=SUPERTYPE_OF, target=fx.b), |
| 63 | + } |
| 64 | + |
| 65 | + def test_unpack_homogenous_tuple_with_prefix_and_suffix(self) -> None: |
| 66 | + fx = self.fx |
| 67 | + assert set( |
| 68 | + infer_constraints( |
| 69 | + Instance(fx.gv2i, [fx.t, UnpackType(Instance(fx.std_tuplei, [fx.s])), fx.u]), |
| 70 | + Instance(fx.gv2i, [fx.a, fx.b, fx.c, fx.d]), |
| 71 | + SUPERTYPE_OF, |
| 72 | + ) |
| 73 | + ) == { |
| 74 | + Constraint(type_var=fx.t, op=SUPERTYPE_OF, target=fx.a), |
| 75 | + Constraint(type_var=fx.s, op=SUPERTYPE_OF, target=fx.b), |
| 76 | + Constraint(type_var=fx.s, op=SUPERTYPE_OF, target=fx.c), |
| 77 | + Constraint(type_var=fx.u, op=SUPERTYPE_OF, target=fx.d), |
| 78 | + } |
| 79 | + |
| 80 | + def test_unpack_tuple(self) -> None: |
| 81 | + fx = self.fx |
| 82 | + assert set( |
| 83 | + infer_constraints( |
| 84 | + Instance( |
| 85 | + fx.gvi, |
| 86 | + [ |
| 87 | + UnpackType( |
| 88 | + TupleType([fx.t, fx.s], fallback=Instance(fx.std_tuplei, [fx.o])) |
| 89 | + ) |
| 90 | + ], |
| 91 | + ), |
| 92 | + Instance(fx.gvi, [fx.a, fx.b]), |
| 93 | + SUPERTYPE_OF, |
| 94 | + ) |
| 95 | + ) == { |
| 96 | + Constraint(type_var=fx.t, op=SUPERTYPE_OF, target=fx.a), |
| 97 | + Constraint(type_var=fx.s, op=SUPERTYPE_OF, target=fx.b), |
| 98 | + } |
| 99 | + |
| 100 | + def test_unpack_with_prefix_and_suffix(self) -> None: |
| 101 | + fx = self.fx |
| 102 | + assert set( |
| 103 | + infer_constraints( |
| 104 | + Instance( |
| 105 | + fx.gv2i, |
| 106 | + [ |
| 107 | + fx.u, |
| 108 | + UnpackType( |
| 109 | + TupleType([fx.t, fx.s], fallback=Instance(fx.std_tuplei, [fx.o])) |
| 110 | + ), |
| 111 | + fx.u, |
| 112 | + ], |
| 113 | + ), |
| 114 | + Instance(fx.gv2i, [fx.a, fx.b, fx.c, fx.d]), |
| 115 | + SUPERTYPE_OF, |
| 116 | + ) |
| 117 | + ) == { |
| 118 | + Constraint(type_var=fx.u, op=SUPERTYPE_OF, target=fx.a), |
| 119 | + Constraint(type_var=fx.t, op=SUPERTYPE_OF, target=fx.b), |
| 120 | + Constraint(type_var=fx.s, op=SUPERTYPE_OF, target=fx.c), |
| 121 | + Constraint(type_var=fx.u, op=SUPERTYPE_OF, target=fx.d), |
| 122 | + } |
| 123 | + |
| 124 | + def test_unpack_tuple_length_non_match(self) -> None: |
| 125 | + fx = self.fx |
| 126 | + assert set( |
| 127 | + infer_constraints( |
| 128 | + Instance( |
| 129 | + fx.gv2i, |
| 130 | + [ |
| 131 | + fx.u, |
| 132 | + UnpackType( |
| 133 | + TupleType([fx.t, fx.s], fallback=Instance(fx.std_tuplei, [fx.o])) |
| 134 | + ), |
| 135 | + fx.u, |
| 136 | + ], |
| 137 | + ), |
| 138 | + Instance(fx.gv2i, [fx.a, fx.b, fx.d]), |
| 139 | + SUPERTYPE_OF, |
| 140 | + ) |
| 141 | + # We still get constraints on the prefix/suffix in this case. |
| 142 | + ) == { |
| 143 | + Constraint(type_var=fx.u, op=SUPERTYPE_OF, target=fx.a), |
| 144 | + Constraint(type_var=fx.u, op=SUPERTYPE_OF, target=fx.d), |
| 145 | + } |
0 commit comments