testconstraints.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. from __future__ import annotations
  2. import pytest
  3. from mypy.constraints import SUBTYPE_OF, SUPERTYPE_OF, Constraint, infer_constraints
  4. from mypy.test.helpers import Suite
  5. from mypy.test.typefixture import TypeFixture
  6. from mypy.types import Instance, TupleType, UnpackType
  7. class ConstraintsSuite(Suite):
  8. def setUp(self) -> None:
  9. self.fx = TypeFixture()
  10. def test_no_type_variables(self) -> None:
  11. assert not infer_constraints(self.fx.o, self.fx.o, SUBTYPE_OF)
  12. def test_basic_type_variable(self) -> None:
  13. fx = self.fx
  14. for direction in [SUBTYPE_OF, SUPERTYPE_OF]:
  15. assert infer_constraints(fx.gt, fx.ga, direction) == [
  16. Constraint(type_var=fx.t, op=direction, target=fx.a)
  17. ]
  18. @pytest.mark.xfail
  19. def test_basic_type_var_tuple_subtype(self) -> None:
  20. fx = self.fx
  21. assert infer_constraints(
  22. Instance(fx.gvi, [UnpackType(fx.ts)]), Instance(fx.gvi, [fx.a, fx.b]), SUBTYPE_OF
  23. ) == [
  24. Constraint(type_var=fx.ts, op=SUBTYPE_OF, target=TupleType([fx.a, fx.b], fx.std_tuple))
  25. ]
  26. def test_basic_type_var_tuple(self) -> None:
  27. fx = self.fx
  28. assert infer_constraints(
  29. Instance(fx.gvi, [UnpackType(fx.ts)]), Instance(fx.gvi, [fx.a, fx.b]), SUPERTYPE_OF
  30. ) == [
  31. Constraint(
  32. type_var=fx.ts, op=SUPERTYPE_OF, target=TupleType([fx.a, fx.b], fx.std_tuple)
  33. )
  34. ]
  35. def test_type_var_tuple_with_prefix_and_suffix(self) -> None:
  36. fx = self.fx
  37. assert set(
  38. infer_constraints(
  39. Instance(fx.gv2i, [fx.t, UnpackType(fx.ts), fx.s]),
  40. Instance(fx.gv2i, [fx.a, fx.b, fx.c, fx.d]),
  41. SUPERTYPE_OF,
  42. )
  43. ) == {
  44. Constraint(type_var=fx.t, op=SUPERTYPE_OF, target=fx.a),
  45. Constraint(
  46. type_var=fx.ts, op=SUPERTYPE_OF, target=TupleType([fx.b, fx.c], fx.std_tuple)
  47. ),
  48. Constraint(type_var=fx.s, op=SUPERTYPE_OF, target=fx.d),
  49. }
  50. def test_unpack_homogenous_tuple(self) -> None:
  51. fx = self.fx
  52. assert set(
  53. infer_constraints(
  54. Instance(fx.gvi, [UnpackType(Instance(fx.std_tuplei, [fx.t]))]),
  55. Instance(fx.gvi, [fx.a, fx.b]),
  56. SUPERTYPE_OF,
  57. )
  58. ) == {
  59. Constraint(type_var=fx.t, op=SUPERTYPE_OF, target=fx.a),
  60. Constraint(type_var=fx.t, op=SUPERTYPE_OF, target=fx.b),
  61. }
  62. def test_unpack_homogenous_tuple_with_prefix_and_suffix(self) -> None:
  63. fx = self.fx
  64. assert set(
  65. infer_constraints(
  66. Instance(fx.gv2i, [fx.t, UnpackType(Instance(fx.std_tuplei, [fx.s])), fx.u]),
  67. Instance(fx.gv2i, [fx.a, fx.b, fx.c, fx.d]),
  68. SUPERTYPE_OF,
  69. )
  70. ) == {
  71. Constraint(type_var=fx.t, op=SUPERTYPE_OF, target=fx.a),
  72. Constraint(type_var=fx.s, op=SUPERTYPE_OF, target=fx.b),
  73. Constraint(type_var=fx.s, op=SUPERTYPE_OF, target=fx.c),
  74. Constraint(type_var=fx.u, op=SUPERTYPE_OF, target=fx.d),
  75. }
  76. def test_unpack_tuple(self) -> None:
  77. fx = self.fx
  78. assert set(
  79. infer_constraints(
  80. Instance(
  81. fx.gvi,
  82. [
  83. UnpackType(
  84. TupleType([fx.t, fx.s], fallback=Instance(fx.std_tuplei, [fx.o]))
  85. )
  86. ],
  87. ),
  88. Instance(fx.gvi, [fx.a, fx.b]),
  89. SUPERTYPE_OF,
  90. )
  91. ) == {
  92. Constraint(type_var=fx.t, op=SUPERTYPE_OF, target=fx.a),
  93. Constraint(type_var=fx.s, op=SUPERTYPE_OF, target=fx.b),
  94. }
  95. def test_unpack_with_prefix_and_suffix(self) -> None:
  96. fx = self.fx
  97. assert set(
  98. infer_constraints(
  99. Instance(
  100. fx.gv2i,
  101. [
  102. fx.u,
  103. UnpackType(
  104. TupleType([fx.t, fx.s], fallback=Instance(fx.std_tuplei, [fx.o]))
  105. ),
  106. fx.u,
  107. ],
  108. ),
  109. Instance(fx.gv2i, [fx.a, fx.b, fx.c, fx.d]),
  110. SUPERTYPE_OF,
  111. )
  112. ) == {
  113. Constraint(type_var=fx.u, op=SUPERTYPE_OF, target=fx.a),
  114. Constraint(type_var=fx.t, op=SUPERTYPE_OF, target=fx.b),
  115. Constraint(type_var=fx.s, op=SUPERTYPE_OF, target=fx.c),
  116. Constraint(type_var=fx.u, op=SUPERTYPE_OF, target=fx.d),
  117. }
  118. def test_unpack_tuple_length_non_match(self) -> None:
  119. fx = self.fx
  120. assert set(
  121. infer_constraints(
  122. Instance(
  123. fx.gv2i,
  124. [
  125. fx.u,
  126. UnpackType(
  127. TupleType([fx.t, fx.s], fallback=Instance(fx.std_tuplei, [fx.o]))
  128. ),
  129. fx.u,
  130. ],
  131. ),
  132. Instance(fx.gv2i, [fx.a, fx.b, fx.d]),
  133. SUPERTYPE_OF,
  134. )
  135. # We still get constraints on the prefix/suffix in this case.
  136. ) == {
  137. Constraint(type_var=fx.u, op=SUPERTYPE_OF, target=fx.a),
  138. Constraint(type_var=fx.u, op=SUPERTYPE_OF, target=fx.d),
  139. }
  140. def test_var_length_tuple_with_fixed_length_tuple(self) -> None:
  141. fx = self.fx
  142. assert not infer_constraints(
  143. TupleType([fx.t, fx.s], fallback=Instance(fx.std_tuplei, [fx.o])),
  144. Instance(fx.std_tuplei, [fx.a]),
  145. SUPERTYPE_OF,
  146. )