Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash involving recursive union of tuples #17353

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
Loading