#4068·Nuitka

Assignment expression in a generator expression binds the target to the module, not the enclosing function

Author: KRRT7Created Sep 30, 2026Updated Sep 30, 2026

Bug Report

Bug Description

An assignment expression inside a generator expression binds its target to the module, not to the enclosing function. Per PEP 572, the target binds in the nearest enclosing scope that isn't a comprehension; here that's the function, where it should be a local.

Compiled code leaves the name in the module's globals. Every call of the function, from every thread, then shares that one variable. List comprehensions are not affected.

️ Environment

1. Nuitka Version, Python Version, OS, and Platform

Reproduced on develop:

4.3rc4
Update status: newer than stable release '4.2.2' (cached, 14 hours old).
Commercial: None
Python: 3.14.7 (main, Sep 24 2026, 17:58:18) [Clang 22.1.3 ]
Flavor: Python Build Standalone
GIL: yes
Executable: ~/scratch/walrus/devvenv/bin/python
OS: Linux
Arch: x86_64
Distribution: Ubuntu (based on Debian) 26.04.1
Version C compiler: gcc (gcc 15).

It also reproduces on the stable release 4.2.2, with the same Python and machine. A free-threaded 3.14.7 build behaves the same way.

2. How Nuitka and Python were Installed

Python is a uv-managed python-build-standalone 3.14.7, in a virtualenv. Nuitka was installed with uv pip install https://github.com/Nuitka/Nuitka/archive/develop.zip, and 4.2.2 from PyPI.

3. Relevant PyPI Packages and Versions

None; the example uses no packages.

️ To Reproduce

1. "Hello World" Test (if applicable)

Not applicable: compilation works, the compiled program's behavior differs from CPython.

2. Short, Self-Contained, Correct, Eligible (SSCCE) Example

python
def f():
    return tuple(x for v in [1, 2] if (x := v * 10))


f()
print("x" in globals())

3. Nuitka Command Line Options

bash
python -m nuitka --mode=standalone repro.py

Expected Behavior

False, as CPython prints: x is a local of f.

Actual Behavior & Output

True

x has become a module variable. In the generated C, the generator expression stores the target with UPDATE_STRING_DICT0(moduledict___main__, ...) and reads it back through the module variable accessor.

The same function as a list comprehension, [y for v in [1, 2] if (y := v * 10)], does not leak y.

↩️ Regression (if applicable)

I haven't checked releases before 4.2.2.

Additional Context (Optional)

Impact. Code of this shape returns another call's value when two threads run it at once, because they overwrite each other's target:

python
def dependencies(values):
    return tuple(
        requirement
        for value in values
        if (requirement := parse(value)) is not None
        if applies(requirement)
    )

With 8 threads calling this on different inputs, a compiled build returned a tuple containing another thread's requirement several times per run. We hit this in a package resolver: a dependency was silently replaced by another package's requirement and then dropped from the result. It's rare with the GIL and frequent on a free-threaded build.

Cause. buildNamedExprNode (nuitka/tree/ReformulationAssignmentStatements.py) adds a non-local declaration for the target to the <genexpr> body, with user_provided=False. When the enclosing function doesn't assign the name anywhere else, it has no such variable. _handleNonLocal (nuitka/tree/VariableClosure.py) then gets a module variable back from takeVariableForClosure. Because the declaration isn't user-provided, it skips the "no binding for nonlocal" error and uses the module variable.

Fix that works for us. In buildNamedExprNode, when the target is inside a generator expression, also make it a variable of the enclosing function: call getVariableForAssignment(name) on the nearest enclosing provider that is neither an outline nor a generator expression.

  • Skip module level, where a module variable is correct.
  • Skip class bodies, which don't allow the construct.
  • Respect a global/nonlocal declaration in that function.
  • Give nested generator expressions the non-local declaration as well.

With that change the output matches CPython for:

  • basic and nested generator expressions;
  • a global declaration in the enclosing function;
  • lambdas and methods;
  • module level;
  • reading the target while the generator is suspended.

Happy to send it as a PR against develop if that's useful.