Skip to content

python: a closure cell rebound through nonlocal from a nested scope is not single-write (#381) - #2023

Merged
swapnilpaliwal-sd merged 3 commits into
0.2.4from
fix/python-nonlocal-rebind
Nov 19, 2026
Merged

swapnilpaliwal-sd merged 3 commits into
0.2.4from
fix/python-nonlocal-rebind

Conversation

@swapnilpaliwal-sd

Copy link
Copy Markdown
Contributor

Fixes #381

What changed

symtable counts a name's binding sites per scope. A closure cell written in its own function (fn = first) and again from a nested function through nonlocal fn; fn = second is two binding rows, and the cell's own row says bindingCount = 1. Every single-write guard (binding_rebinding(p, "1", ...)) read that row, so the value flow committed to the first write.

  • binding_nonlocal_write(p, cell, nb): an ASSIGNED NONLOCAL binding nb whose LEGB walk (binding_lookup) lands on cell. A nonlocal that only reads the cell is not a write.
  • binding_rebinding is now derived in resolution/name-resolution.dl: the row's count plus the number of nested writes. Every guard reads it unchanged.
  • The projection keeps the per-row count as binding_rebinding_raw. Only the LEGB walk reads it (its cnt != "0" test), because binding_rebinding is derived from that walk, so the stratification holds.
  • binding_is_assigned projects isAssigned.

The call site is now a declared unknown (ambiguous_unknown, no_rule) instead of known_edge -> first. Unioning the writes into multi_inferred {first, second} is left out on purpose. Without a check that every write resolves, a nested write the engine cannot read (fn = make()) would leave a one-element set, which tiers as known_edge -> first again. That check needs negation or an aggregate over expr_denotes_method inside the resolution fixpoint, so it is a separate change.

Why the goldens moved, or why they did not

New case 44-nonlocal-rebind. No existing golden moved: every Python case, both whole-project fixtures and the torture set match as before.

Evidence

def nonlocal_rebind():
    fn = first
    def swap():
        nonlocal fn
        fn = second
    swap()
    return fn()          # runs second
site before after
fn() in nonlocal_rebind known_edge -> first ambiguous_unknown
fn() after a nonlocal fn that only reads (control) known_edge -> first known_edge -> first
fn() beside a nested local of the same name (control) known_edge -> first known_edge -> first

graph/test/python/run-tests.sh: passed 44, failed 0 (41 cases, both project fixtures, torture).

@swapnilpaliwal-sd swapnilpaliwal-sd added bug Something isn't working engine Resolution / call-graph engine rules labels Nov 18, 2026
@swapnilpaliwal-sd swapnilpaliwal-sd added the python Python label Nov 18, 2026
…s not single-write (#381)

symtable counts a name's binding sites per scope, so `fn = first` in the enclosing
function and `nonlocal fn; fn = second` in a nested one are two rows, the cell's
own saying 1. Every single-write guard read that row, so `swap(); fn()` came out
known_edge -> first, a function that cannot run there, and `second` was the
target of no edge.

binding_rebinding now adds to a cell's count each ASSIGNED NONLOCAL binding whose
LEGB walk lands on it (binding_nonlocal_write). The raw per-row count is kept as
binding_rebinding_raw for the walk itself, which binding_rebinding is derived
from. The site is now a declared unknown instead of a wrong committed target. A
`nonlocal` that only reads the cell, and a nested local of the same name, keep
their single-write answers (both are controls in the new case).

Case 44-nonlocal-rebind. No other golden moved.
… keeps its own version)

0.2.3 is released, so what merges into this branch ships in 0.2.4. package.json (version and the five @axiomcode/engine-* pins) and the four editor plugin and extension manifests move from 0.2.3 to 0.2.4. The parser keeps its own version.
@swapnilpaliwal-sd
swapnilpaliwal-sd merged commit f5868f8 into 0.2.4 Nov 19, 2026
13 checks passed
@swapnilpaliwal-sd
swapnilpaliwal-sd deleted the fix/python-nonlocal-rebind branch November 19, 2026 04:31
@swapnilpaliwal-sd swapnilpaliwal-sd mentioned this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working engine Resolution / call-graph engine rules python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

python: a cell rebound by nonlocal from a nested scope still counts as single-write, so the call commits to the pre-rebind function

1 participant