Skip to content

Commit a7f52db

Browse files
ilevkivskyijhance
authored andcommitted
Fix crash on joins with recursive tuples (#15402)
Fixes #15351 This may have some perf impact, but I predict it will be minimal (and I didn't notice anything locally).
1 parent 3bf9fdc commit a7f52db

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

mypy/typeops.py

+6
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,18 @@ def is_recursive_pair(s: Type, t: Type) -> bool:
7676
isinstance(get_proper_type(t), (Instance, UnionType))
7777
or isinstance(t, TypeAliasType)
7878
and t.is_recursive
79+
# Tuple types are special, they can cause an infinite recursion even if
80+
# the other type is not recursive, because of the tuple fallback that is
81+
# calculated "on the fly".
82+
or isinstance(get_proper_type(s), TupleType)
7983
)
8084
if isinstance(t, TypeAliasType) and t.is_recursive:
8185
return (
8286
isinstance(get_proper_type(s), (Instance, UnionType))
8387
or isinstance(s, TypeAliasType)
8488
and s.is_recursive
89+
# Same as above.
90+
or isinstance(get_proper_type(t), TupleType)
8591
)
8692
return False
8793

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

+9
Original file line numberDiff line numberDiff line change
@@ -937,3 +937,12 @@ x: A[int, str]
937937
if last is not None:
938938
reveal_type(last) # N: Revealed type is "Tuple[builtins.int, builtins.str, Union[Tuple[builtins.int, builtins.str, Union[..., None]], None]]"
939939
[builtins fixtures/tuple.pyi]
940+
941+
[case testRecursiveAliasLiteral]
942+
from typing import Tuple
943+
from typing_extensions import Literal
944+
945+
NotFilter = Tuple[Literal["not"], "NotFilter"]
946+
n: NotFilter
947+
reveal_type(n[1][1][0]) # N: Revealed type is "Literal['not']"
948+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)