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
inlined comprehension and closed over variables have buggy interactions #156664
this passes fine in CPython 3.11 but breaks like this in 3.12 onwards, including on main:
$ ./python /tmp/bug.py
Traceback (most recent call last):
File "/tmp/bug.py", line 10, in <module>
test_inlined_comprehension_private_name_reuse()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/tmp/bug.py", line 8, in test_inlined_comprehension_private_name_reuse
assert f() == [3]
~^^
File "/tmp/bug.py", line 6, in f
return [x for _ in [1]]
^
UnboundLocalError: cannot access local variable 'x' where it is not associated with a value
I wonder whether that is the kind of bug that would be easier to fix when doing #124697
Activity
iritkatriel commentedon Aug 30, 2026
CC @carljm
JelleZijlstra commentedon Aug 30, 2026
Surprised this has never been caught before! I even ran a fuzzer when we were implementing PEP 709 to find things like this. I'm attempting a fix now.
JelleZijlstra commentedon Aug 30, 2026
An analogous case that is also broken in 3.12+:
And a more complicated version:
pythongh-156664: Preserve enclosing cells in nested comprehensions
pythongh-156664: Deduplicate comprehension frame locals