Skip to content

Commit 5ae9e69

Browse files
authored
Fix crash involving recursive union of tuples (#17353)
Fixes #17236 It turns out we were calculating tuple fallbacks where we don't really need to. We can rely on the fact that tuple fallback is trivial for non-trivial partial fallbacks to simplify the logic and avoid the infinite recursion.
1 parent 7c391dd commit 5ae9e69

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

mypy/subtypes.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -794,15 +794,18 @@ def visit_tuple_type(self, left: TupleType) -> bool:
794794
return False
795795
if any(not self._is_subtype(l, r) for l, r in zip(left.items, right.items)):
796796
return False
797-
rfallback = mypy.typeops.tuple_fallback(right)
798-
if is_named_instance(rfallback, "builtins.tuple"):
797+
if is_named_instance(right.partial_fallback, "builtins.tuple"):
799798
# No need to verify fallback. This is useful since the calculated fallback
800799
# may be inconsistent due to how we calculate joins between unions vs.
801800
# non-unions. For example, join(int, str) == object, whereas
802801
# join(Union[int, C], Union[str, C]) == Union[int, str, C].
803802
return True
804-
lfallback = mypy.typeops.tuple_fallback(left)
805-
return self._is_subtype(lfallback, rfallback)
803+
if is_named_instance(left.partial_fallback, "builtins.tuple"):
804+
# Again, no need to verify. At this point we know the right fallback
805+
# is a subclass of tuple, so if left is plain tuple, it cannot be a subtype.
806+
return False
807+
# At this point we know both fallbacks are non-tuple.
808+
return self._is_subtype(left.partial_fallback, right.partial_fallback)
806809
else:
807810
return False
808811

test-data/unit/check-recursive-types.test

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,3 +958,39 @@ z: Z
958958
x: X
959959
reveal_type(z) # N: Revealed type is "Union[__main__.A[...], __main__.B[Union[..., None]]]"
960960
reveal_type(x) # N: Revealed type is "Union[__main__.A[Union[..., None]], __main__.B[Union[..., None]]]"
961+
962+
[case testRecursiveTupleFallback1]
963+
from typing import NewType, Tuple, Union
964+
965+
T1 = NewType("T1", str)
966+
T2 = Tuple[T1, "T4", "T4"]
967+
T3 = Tuple[str, "T4", "T4"]
968+
T4 = Union[T2, T3]
969+
[builtins fixtures/tuple.pyi]
970+
971+
[case testRecursiveTupleFallback2]
972+
from typing import NewType, Tuple, Union
973+
974+
T1 = NewType("T1", str)
975+
class T2(Tuple[T1, "T4", "T4"]): ...
976+
T3 = Tuple[str, "T4", "T4"]
977+
T4 = Union[T2, T3]
978+
[builtins fixtures/tuple.pyi]
979+
980+
[case testRecursiveTupleFallback3]
981+
from typing import NewType, Tuple, Union
982+
983+
T1 = NewType("T1", str)
984+
T2 = Tuple[T1, "T4", "T4"]
985+
class T3(Tuple[str, "T4", "T4"]): ...
986+
T4 = Union[T2, T3]
987+
[builtins fixtures/tuple.pyi]
988+
989+
[case testRecursiveTupleFallback4]
990+
from typing import NewType, Tuple, Union
991+
992+
T1 = NewType("T1", str)
993+
class T2(Tuple[T1, "T4", "T4"]): ...
994+
class T3(Tuple[str, "T4", "T4"]): ...
995+
T4 = Union[T2, T3]
996+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)