Skip to content

Commit 35679e2

Browse files
authored
Fix another crash scenario on recursive tuple types (#17708)
Fixes #17691 This looks quite ad-hoc, but the only clean alternative is to remove the existing recursive types optimization we have in `subtypes.py`. But the best I can get without that optimization is -7% performance penalty (on self-check). So I think it is not worth it, since it is still a corner case.
1 parent fe15ee6 commit 35679e2

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

mypy/typeops.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ def tuple_fallback(typ: TupleType) -> Instance:
114114
else:
115115
items.append(item)
116116
return Instance(
117-
info, [make_simplified_union(items)], extra_attrs=typ.partial_fallback.extra_attrs
117+
info,
118+
# Note: flattening recursive unions is dangerous, since it can fool recursive
119+
# types optimization in subtypes.py and go into infinite recursion.
120+
[make_simplified_union(items, handle_recursive=False)],
121+
extra_attrs=typ.partial_fallback.extra_attrs,
118122
)
119123

120124

@@ -440,6 +444,7 @@ def make_simplified_union(
440444
*,
441445
keep_erased: bool = False,
442446
contract_literals: bool = True,
447+
handle_recursive: bool = True,
443448
) -> ProperType:
444449
"""Build union type with redundant union items removed.
445450
@@ -465,7 +470,7 @@ def make_simplified_union(
465470
to_union().
466471
"""
467472
# Step 1: expand all nested unions
468-
items = flatten_nested_unions(items)
473+
items = flatten_nested_unions(items, handle_recursive=handle_recursive)
469474

470475
# Step 2: fast path for single item
471476
if len(items) == 1:

mypy/types.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3629,7 +3629,7 @@ def extend_args_for_prefix_and_suffix(
36293629

36303630

36313631
def flatten_nested_unions(
3632-
types: Sequence[Type], handle_type_alias_type: bool = True
3632+
types: Sequence[Type], *, handle_type_alias_type: bool = True, handle_recursive: bool = True
36333633
) -> list[Type]:
36343634
"""Flatten nested unions in a type list."""
36353635
if not isinstance(types, list):
@@ -3643,7 +3643,13 @@ def flatten_nested_unions(
36433643

36443644
flat_items: list[Type] = []
36453645
for t in typelist:
3646-
tp = get_proper_type(t) if handle_type_alias_type else t
3646+
if handle_type_alias_type:
3647+
if not handle_recursive and isinstance(t, TypeAliasType) and t.is_recursive:
3648+
tp: Type = t
3649+
else:
3650+
tp = get_proper_type(t)
3651+
else:
3652+
tp = t
36473653
if isinstance(tp, ProperType) and isinstance(tp, UnionType):
36483654
flat_items.extend(
36493655
flatten_nested_unions(tp.items, handle_type_alias_type=handle_type_alias_type)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,3 +994,15 @@ class T2(Tuple[T1, "T4", "T4"]): ...
994994
class T3(Tuple[str, "T4", "T4"]): ...
995995
T4 = Union[T2, T3]
996996
[builtins fixtures/tuple.pyi]
997+
998+
[case testRecursiveTupleFallback5]
999+
from typing import Protocol, Tuple, Union
1000+
1001+
class Proto(Protocol):
1002+
def __len__(self) -> int: ...
1003+
1004+
A = Union[Proto, Tuple[A]]
1005+
ta: Tuple[A]
1006+
p: Proto
1007+
p = ta
1008+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)