-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Recursion error with nested Union #9657
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
Comments
That's fun; I can repro. Thanks for the clear report. |
Not that this isn't a bug, but note that Iterator is a subclass of Iterable (https://github.com/python/typeshed/blob/master/stdlib/3/typing.pyi#L177; every Iterator is also an Iterable), so the Union in your original example is redundant. |
Thank you @JelleZijlstra. If I had checked collections.abc I would have seen this too, I feel like a bit of a numpty now. Thank you I'll shift over to that. Tests to check Iterable = Iterdef foo_list() -> Iterable[int]:
return [0]
def foo_set() -> Iterable[int]:
return {0}
def foo_dict() -> Iterable[int]:
return {0: 'a'}
def foo_iterator() -> Iterable[int]:
yield 0
def bar(foo: Iterable[int]) -> None:
pass
bar([0])
bar({0})
bar({0: 'a'})
bar(iter([0])) In light of this I tested changing to from typing import Union, Iterator, Iterable
def bar(
values: Union[
Iterable[Union[
Iterable[Union[Iterable[int], Iterator[int]]],
Iterator[Union[Iterable[int], Iterator[int]]],
]],
Iterator[Union[
Iterable[Union[Iterable[int], Iterator[int]]],
Iterator[Union[Iterable[int], Iterator[int]]],
]],
]
) -> Iterator[int]:
for i in values:
for j in i:
for k in j:
yield k
bar([[[1]]]) |
Uh oh!
There was an error while loading. Please reload this page.
Crash Report
Many of my functions take
Iter = Union[Iterator[T], Iterable[T]]
as arguments. Asfor
and many other functions in Python work with both iterators and iterables. However when I useIter[Iter[T]]
the code can break.I've used
Iter[Iter[Iter[int]]]
as an example asIter[Iter[T]]
didn't break locally on the MVCE. Possibly due to a smaller existing stack.Traceback
Traceback - it's long
To Reproduce
Playground. However, I'm not sure if these are the same errors. My local mypy results in a status 2 where the playground has status 139.
Existing issues
This error seems related to #6730 however the tracebacks are different. Whilst the crashes may be related I think this shows that the current code is exponential leading to crashes.
My recursive traceback
6730 recursive traceback
Caching
The error seems somewhat tempermental. If
bar
is proceeded with a subset of the type it seems some caching comes in to play. This runs without error.The text was updated successfully, but these errors were encountered: