Repository navigation
python: a closure cell rebound through nonlocal from a nested scope is not single-write (#381) - #2023
Merged
Merged
Conversation
swapnilpaliwal-sd
requested review from
JaredHLZhang and
suyashpaliwal26
as code owners
November 18, 2026 23:05
…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.
…thon-nonlocal-rebind
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 throughnonlocal fn; fn = secondis two binding rows, and the cell's own row saysbindingCount = 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 ASSIGNEDNONLOCALbindingnbwhose LEGB walk (binding_lookup) lands oncell. Anonlocalthat only reads the cell is not a write.binding_rebindingis now derived inresolution/name-resolution.dl: the row's count plus the number of nested writes. Every guard reads it unchanged.binding_rebinding_raw. Only the LEGB walk reads it (itscnt != "0"test), becausebinding_rebindingis derived from that walk, so the stratification holds.binding_is_assignedprojectsisAssigned.The call site is now a declared unknown (
ambiguous_unknown,no_rule) instead ofknown_edge -> first. Unioning the writes intomulti_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 asknown_edge -> firstagain. That check needs negation or an aggregate overexpr_denotes_methodinside 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
fn()innonlocal_rebindknown_edge -> firstambiguous_unknownfn()after anonlocal fnthat only reads (control)known_edge -> firstknown_edge -> firstfn()beside a nested local of the same name (control)known_edge -> firstknown_edge -> firstgraph/test/python/run-tests.sh: passed 44, failed 0 (41 cases, both project fixtures, torture).