Skip to content

Commit

Permalink
Fix another crash scenario on recursive tuple types (#17708)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ilevkivskyi committed Aug 30, 2024
1 parent fe15ee6 commit 35679e2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
9 changes: 7 additions & 2 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ def tuple_fallback(typ: TupleType) -> Instance:
else:
items.append(item)
return Instance(
info, [make_simplified_union(items)], extra_attrs=typ.partial_fallback.extra_attrs
info,
# Note: flattening recursive unions is dangerous, since it can fool recursive
# types optimization in subtypes.py and go into infinite recursion.
[make_simplified_union(items, handle_recursive=False)],
extra_attrs=typ.partial_fallback.extra_attrs,
)


Expand Down Expand Up @@ -440,6 +444,7 @@ def make_simplified_union(
*,
keep_erased: bool = False,
contract_literals: bool = True,
handle_recursive: bool = True,
) -> ProperType:
"""Build union type with redundant union items removed.
Expand All @@ -465,7 +470,7 @@ def make_simplified_union(
to_union().
"""
# Step 1: expand all nested unions
items = flatten_nested_unions(items)
items = flatten_nested_unions(items, handle_recursive=handle_recursive)

# Step 2: fast path for single item
if len(items) == 1:
Expand Down
10 changes: 8 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3629,7 +3629,7 @@ def extend_args_for_prefix_and_suffix(


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

flat_items: list[Type] = []
for t in typelist:
tp = get_proper_type(t) if handle_type_alias_type else t
if handle_type_alias_type:
if not handle_recursive and isinstance(t, TypeAliasType) and t.is_recursive:
tp: Type = t
else:
tp = get_proper_type(t)
else:
tp = t
if isinstance(tp, ProperType) and isinstance(tp, UnionType):
flat_items.extend(
flatten_nested_unions(tp.items, handle_type_alias_type=handle_type_alias_type)
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-recursive-types.test
Original file line number Diff line number Diff line change
Expand Up @@ -994,3 +994,15 @@ class T2(Tuple[T1, "T4", "T4"]): ...
class T3(Tuple[str, "T4", "T4"]): ...
T4 = Union[T2, T3]
[builtins fixtures/tuple.pyi]

[case testRecursiveTupleFallback5]
from typing import Protocol, Tuple, Union

class Proto(Protocol):
def __len__(self) -> int: ...

A = Union[Proto, Tuple[A]]
ta: Tuple[A]
p: Proto
p = ta
[builtins fixtures/tuple.pyi]

0 comments on commit 35679e2

Please sign in to comment.