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 another crash scenario on recursive tuple types #17708

Merged
merged 2 commits into from
Aug 30, 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
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]
Loading