8000 Fix recursion issue with nested instances and unions by Peilonrayz · Pull Request #9663 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix recursion issue with nested instances and unions #9663

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

Merged
merged 4 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix recursion issue with nested instances and unions
  • Loading branch information
Peilonrayz committed Oct 30, 2020
commit 6b6ebbfc7ff230a7654ea0f450bcbef327634b58
1 change: 0 additions & 1 deletion mypy/sametypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def is_same_type(left: Type, right: Type) -> bool:
# types can be simplified to non-union types such as Union[int, bool]
# -> int. It would be nice if we always had simplified union types but
# this is currently not the case, though it often is.
left = simplify_union(left)
right = simplify_union(right)

return left.accept(SameTypeVisitor(right))
Expand Down
57 changes: 57 additions & 0 deletions test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1059,3 +1059,60 @@ x: Union[None, Any]
y: Union[int, None]
reveal_type(f(x, y)) # N: Revealed type is 'Union[None, Any, builtins.int]'
reveal_type(f(y, x)) # N: Revealed type is 'Union[builtins.int, None, Any]'

[case testNestedProtocolUnions]
from typing import Union, Iterator, Iterable
def foo(
values: Union[
Iterator[Union[
Iterator[Union[Iterator[int], Iterable[int]]],
Iterable[Union[Iterator[int], Iterable[int]]],
]],
Iterable[Union[
Iterator[Union[Iterator[int], Iterable[int]]],
Iterable[Union[Iterator[int], Iterable[int]]],
]],
]
) -> Iterator[int]:
for i in values:
for j in i:
for k in j:
yield k
foo([[[1]]])
[builtins fixtures/list.pyi]

[case testNestedProtocolGenericUnions]
from typing import Union, Iterator, List
def foo(
values: Union[
Iterator[Union[
Iterator[Union[Iterator[int], List[int]]],
List[Union[Iterator[int], List[int]]],
]],
List[Union[
Iterator[Union[Iterator[int], List[int]]],
List[Union[Iterator[int], List[int]]],
]],
]
) -> Iterator[int]:
for i in values:
for j in i:
for k in j:
yield k
foo([[[1]]])
[builtins fixtures/list.pyi]

[case testNestedProtocolGenericUnionsDeep]
from typing import TypeVar, Union, Iterator, List
T = TypeVar("T")
Iter = Union[Iterator[T], List[T]]
def foo(
values: Iter[Iter[Iter[Iter[Iter[int]]]]]) -> Iterator[int]:
for i in values:
for j in i:
for k in j:
for l in k:
for m in l:
yield m
foo([[[[[1]]]]])
[builtins fixtures/list.pyi]
0