Skip to content

fix(names): a module's name is unbound until its assignment runs - #236

Merged
jonruttan merged 2 commits into
mainfrom
fix/annotate-var
Nov 18, 2026
Merged

jonruttan merged 2 commits into
mainfrom
fix/annotate-var

Conversation

@jonruttan

Copy link
Copy Markdown
Owner

A module's assigned names were declared as None before its first statement, so "x" in globals() was True ahead of x = 1, and reading x there gave None instead of NameError. This was the cause of basics/annotate_var. The bare annotation itself was already a no-op; the program assigns x further down.

  • Module targets now start unbound (%py-deleted). %py-decls in python/parse.x does this.
  • A read of one compiles to the existing %py-name-live check only where the name is not bound for certain. That reuses %py-bound-now, which now keeps the module's names as well as a def's. Reads after an unconditional top-level assignment, including those in defs written after it, stay bare symbols. A binding the analysis misses costs a check, never a wrong answer.
  • None += 1 puts None's value in the target list, so the list keeps symbols only. Without that, hoisting it unbinds None for the whole process (exit 139 in 10-assign).

Tests:

  • 07-names has two new cases, with output from CPython 3.14: globals() before and after assignment; NameError for an early read, an if False: assignment, and a def called before its global exists.
  • B2-scoping's annotation case had stated the old behaviour as a DIVERGENCE. It now expects CPython's False/True.

Verified on v0.55.0:

  • All 129 spec files pass: 1,200 tests, 0 failed.
  • basics/annotate_var passes.

🤖 Generated with Claude Code

Every name a module assigns was declared as None before the first
statement, so `"x" in globals()` was True ahead of `x = 1` and a read
there answered None where CPython raises NameError.  Those names now
start as %py-deleted, and a read of one goes through the NameError
check only where it is not bound for certain (%py-bound-now, which now
keeps the module's names too); after an unconditional assignment the
read is the bare symbol it was.

`None += 1` names None, which reads as its value: the target list keeps
symbols only, or hoisting it unbinds None itself.

Makes basics/annotate_var pass and retires B2-scoping's divergence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonruttan
jonruttan merged commit 2abd48c into main Nov 18, 2026
12 checks passed
@jonruttan
jonruttan deleted the fix/annotate-var branch November 18, 2026 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant