You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Behavior of generator expressions doesn't match the docs when passed a non-iterable #143493
According to the docs (and in earlier versions) the correct behavior is:
>>> def f(s):
... return (x for x in s)
...
>>> f(1)
TypeError: 'int' object is not iterable
The current behavior on main is arguably more logical as it is consistent with generators, but Hyrum's law dictates we need to be backwards compatible.
This issue has a bit of history.
To fix #125038 we moved the GET_ITER instruction into the generator body. This broke stuff, so we added a supposedly redundant GET_ITER to the callsite, but that also caused problems #127682.
Eventually, for 3.14 we added an additional check in FOR_ITER that the iterator is indeed an iterator.
This adds a little bit of unnecessary overhead and is an obstacle to optimization.
On main, we use "virtual iterators" and have kept the GET_ITER in the body of the generator. This is efficient and optimizes nicely, but doesn't conform to the docs.
We can keep both the nice performance and conform to the docs, by keeping the GET_ITER in the generator function, but moving it into generator creation and out of the generator body.
Instead of:
just so I understand the proposed bytecode flow correctly: the plan is to have GET_ITER execute before RETURN_GENERATOR, which restores the eager TypeError while preserving the optimized frame behavior for the rest of the generator body.
If that's the case, I'd be happy to write a regression test case for this eager failure behavior to help verify the fix once it lands.
If that's the case, I'd be happy to write a regression test case for this eager failure behavior to help verify the fix once it lands.
If a fix lands, the regression test would be written at the same time. We don't separate tests from fixes. I also don't think it's an issue for newcomers considering the long history of how generator expressions behave.
changed the title [-]Behavior of generator expressions doesn't match the docs when passes a non-iterable sequence.[/-][+]Behavior of generator expressions doesn't match the docs when passed a non-iterable[/+]on Jan 7, 2026
Activity
Yashp002 commentedon Jan 6, 2026
just so I understand the proposed bytecode flow correctly: the plan is to have GET_ITER execute before RETURN_GENERATOR, which restores the eager TypeError while preserving the optimized frame behavior for the rest of the generator body.
If that's the case, I'd be happy to write a regression test case for this eager failure behavior to help verify the fix once it lands.
picnixz commentedon Jan 6, 2026
If a fix lands, the regression test would be written at the same time. We don't separate tests from fixes. I also don't think it's an issue for newcomers considering the long history of how generator expressions behave.
[-]Behavior of generator expressions doesn't match the docs when passes a non-iterable sequence.[/-][+]Behavior of generator expressions doesn't match the docs when passed a non-iterable[/+]sobolevn commentedon Jan 9, 2026
cc @efimov-mikhail
GH-143493: Conform to spec for generator expressions while supporting…
pythonGH-143493: Conform to spec for generator expressions while supp…
gh-143493: fix cleanup on errors in codegen_comprehension (#156374)
pythongh-143493: fix cleanup on errors in codegen_comprehension (pyth…
[3.15] gh-143493: fix cleanup on errors in codegen_comprehension (GH-…