inlined comprehension and closed over variables have buggy interactions
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
We are a bit late to report this, but the following of PyPy's unit tests got broken by inlined comprehensions in CPython 3.12:
def test_inlined_comprehension_private_name_reuse():
x = 3
def f():
[x for x in [1]]
return [x for _ in [1]]
assert f() == [3]
test_inlined_comprehension_private_name_reuse()
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
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
- gh-156691
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided /tmp/bug.py reproducer on CPython main and comparing it with Python 3.11. Read the interaction with issue #124697 and the linked PR gh-156691 before investigating. Done means the example returns [3] without UnboundLocalError while preserving inlined-comprehension behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100