python / python/cpython

inlined comprehension and closed over variables have buggy interactions

Open
#156,664 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-bug
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.