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

StopIteration sometimes gets transformed to RuntimeError (PEP 0479) #321

Open
Hnasar opened this issue Dec 4, 2023 · 0 comments
Open

StopIteration sometimes gets transformed to RuntimeError (PEP 0479) #321

Hnasar opened this issue Dec 4, 2023 · 0 comments

Comments

@Hnasar
Copy link

Hnasar commented Dec 4, 2023

next on an empty iter raises StopIteration

>>> next(iter([]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

And if you do this in a list comprehension you get the same error

>>> x, y = [next(iter(z)) for z in ([], [])]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
StopIteration

But if you change list comprehension to a generator expression then the exception type is transformed!

>>> x, y = (next(iter(z)) for z in ([], []))
Traceback (most recent call last):
  File "<stdin>", line 1, in <genexpr>
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: generator raised StopIteration

This is new behavior as of Python 3.5 https://peps.python.org/pep-0479/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
@Hnasar @satwikkansal and others