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 on nested unions in recursive types #14007

Merged
merged 1 commit into from Nov 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions mypy/typeops.py
Expand Up @@ -71,13 +71,13 @@ def is_recursive_pair(s: Type, t: Type) -> bool:
"""
if isinstance(s, TypeAliasType) and s.is_recursive:
return (
isinstance(get_proper_type(t), Instance)
isinstance(get_proper_type(t), (Instance, UnionType))
or isinstance(t, TypeAliasType)
and t.is_recursive
)
if isinstance(t, TypeAliasType) and t.is_recursive:
return (
isinstance(get_proper_type(s), Instance)
isinstance(get_proper_type(s), (Instance, UnionType))
or isinstance(s, TypeAliasType)
and s.is_recursive
)
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-recursive-types.test
Expand Up @@ -808,3 +808,21 @@ def test2() -> Tree2:
def test3() -> Tree3:
return 42 # E: Incompatible return value type (got "int", expected "Union[str, Tuple[Tree3, Tree3, Tree3]]")
[builtins fixtures/tuple.pyi]

[case testRecursiveDoubleUnionNoCrash]
from typing import Tuple, Union, Callable, Sequence

K = Union[int, Tuple[Union[int, K]]]
L = Union[int, Callable[[], Union[int, L]]]
M = Union[int, Sequence[Union[int, M]]]

x: K
x = x
y: L
y = y
z: M
z = z

x = y # E: Incompatible types in assignment (expression has type "L", variable has type "K")
z = x # OK
[builtins fixtures/tuple.pyi]