Skip to content

Commit

Permalink
Fix crash involving recursive union of tuples (#17353)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ilevkivskyi committed Jun 9, 2024
1 parent 7c391dd commit 5ae9e69
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
11 changes: 7 additions & 4 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,15 +794,18 @@ def visit_tuple_type(self, left: TupleType) -> bool:
return False
if any(not self._is_subtype(l, r) for l, r in zip(left.items, right.items)):
return False
rfallback = mypy.typeops.tuple_fallback(right)
if is_named_instance(rfallback, "builtins.tuple"):
if is_named_instance(right.partial_fallback, "builtins.tuple"):
# No need to verify fallback. This is useful since the calculated fallback
# may be inconsistent due to how we calculate joins between unions vs.
# non-unions. For example, join(int, str) == object, whereas
# join(Union[int, C], Union[str, C]) == Union[int, str, C].
return True
lfallback = mypy.typeops.tuple_fallback(left)
return self._is_subtype(lfallback, rfallback)
if is_named_instance(left.partial_fallback, "builtins.tuple"):
# Again, no need to verify. At this point we know the right fallback
# is a subclass of tuple, so if left is plain tuple, it cannot be a subtype.
return False
# At this point we know both fallbacks are non-tuple.
return self._is_subtype(left.partial_fallback, right.partial_fallback)
else:
return False

Expand Down
36 changes: 36 additions & 0 deletions test-data/unit/check-recursive-types.test
Original file line number Diff line number Diff line change
Expand Up @@ -958,3 +958,39 @@ z: Z
x: X
reveal_type(z) # N: Revealed type is "Union[__main__.A[...], __main__.B[Union[..., None]]]"
reveal_type(x) # N: Revealed type is "Union[__main__.A[Union[..., None]], __main__.B[Union[..., None]]]"

[case testRecursiveTupleFallback1]
from typing import NewType, Tuple, Union

T1 = NewType("T1", str)
T2 = Tuple[T1, "T4", "T4"]
T3 = Tuple[str, "T4", "T4"]
T4 = Union[T2, T3]
[builtins fixtures/tuple.pyi]

[case testRecursiveTupleFallback2]
from typing import NewType, Tuple, Union

T1 = NewType("T1", str)
class T2(Tuple[T1, "T4", "T4"]): ...
T3 = Tuple[str, "T4", "T4"]
T4 = Union[T2, T3]
[builtins fixtures/tuple.pyi]

[case testRecursiveTupleFallback3]
from typing import NewType, Tuple, Union

T1 = NewType("T1", str)
T2 = Tuple[T1, "T4", "T4"]
class T3(Tuple[str, "T4", "T4"]): ...
T4 = Union[T2, T3]
[builtins fixtures/tuple.pyi]

[case testRecursiveTupleFallback4]
from typing import NewType, Tuple, Union

T1 = NewType("T1", str)
class T2(Tuple[T1, "T4", "T4"]): ...
class T3(Tuple[str, "T4", "T4"]): ...
T4 = Union[T2, T3]
[builtins fixtures/tuple.pyi]

0 comments on commit 5ae9e69

Please sign in to comment.