Fix locals()/vars() cell-variable sync for module/class scope - #8467
Merged
youknowone merged 1 commit intoAug 8, 2026
Conversation
…tside function scope CPython's _PyFrame_GetLocals only syncs fastlocals/cells into locals() for function scope; module/class scope just returns the namespace dict as-is. sync_visible_locals_to_mapping did this for every scope, so the implicit __conditional_annotations__ cell (used for PEP 649/749 deferred annotations) broke two ways: at module scope its cell is always empty, so syncing overwrote the dict's real value with None, causing NameError on the next annotated statement; at class scope its cell is the only real value, so syncing leaked it into locals()/dir(), unlike CPython. Skip cell/free slots outside function scope to match CPython, and add a regression snippet. Closes RustPython#8379 Assisted-by: Claude Code:claude-sonnet-5
Contributor
📝 WalkthroughWalkthrough
ChangesLocals synchronization
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related issues
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
kyokuping
pushed a commit
to kyokuping/RustPython
that referenced
this pull request
Aug 9, 2026
…tside function scope (RustPython#8467) CPython's _PyFrame_GetLocals only syncs fastlocals/cells into locals() for function scope; module/class scope just returns the namespace dict as-is. sync_visible_locals_to_mapping did this for every scope, so the implicit __conditional_annotations__ cell (used for PEP 649/749 deferred annotations) broke two ways: at module scope its cell is always empty, so syncing overwrote the dict's real value with None, causing NameError on the next annotated statement; at class scope its cell is the only real value, so syncing leaked it into locals()/dir(), unlike CPython. Skip cell/free slots outside function scope to match CPython, and add a regression snippet. Closes RustPython#8379 Assisted-by: Claude Code:claude-sonnet-5
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.
Summary
_PyFrame_GetLocalsonly syncs fastlocals/cells intolocals()for function scope; for module/class scope it just returns the namespace dict as-is, since ordinary names there are already stored viaStoreName, not fastlocals.sync_visible_locals_to_mappingdid the fastlocals/cell sync for every scope, so the implicit__conditional_annotations__cell (added for PEP 649/749 deferred annotations) broke two ways:1. At module scope its cell is always empty (real writes go through
StoreName), so syncing overwrote the dict's real value withNone, deleting it and causingNameErroron the next annotated statement.2. At class scope the cell is the only place the value lives (
StoreDeref-only, never in the dict), so syncing instead leaked__conditional_annotations__intolocals()/dir(), which CPython never does.Fix
Skip
CO_FAST_CELL/CO_FAST_FREEslots for non-optimized (module/class) scope insync_visible_locals_to_mapping, matching CPython's behavior.Details
v3.14.6tag): module codegen writes__conditional_annotations__withStoreNameinto the dict; class codegen writes it withStoreDerefinto the cell only._PyFrame_GetLocalsnever reads cells for either scope — it returns the namespace dict directly for non-optimized scope.CO_FAST_CELL/CO_FAST_FREE, notCO_FAST_HIDDEN— a hidden slot (PEP 709 inlined comprehensions) can also be a cell, and the existing hidden-variable check just above already decides per-slot whether a live hidden value should still appear inlocals(). Gating on scope alone would have overridden that.Testing
extra_tests/snippets/syntax_annotations_locals.py, covering both the module-scope corruption and the class-scope leak. Confirmed it fails with the pre-fixNameErroron unfixed code and passes after the fix.cargo test -p rustpython-codegen(783 tests),cargo test -p rustpython-vm --lib(55 tests): all pass.test_annotationlib(117/117),test_builtin(138/138),test_class(37/37) all pass.cargo clippy/cargo fmt --check: clean.AI assistance
Investigated, implemented, and tested with Claude Code assistance, including direct comparison against a local CPython 3.14.6 source checkout and binary; all findings and the fix were manually verified before submission.
Assisted-by: Claude Code:claude-sonnet-5
Summary by CodeRabbit
Bug Fixes
locals()behavior in module and class scopes to match Python semantics.exec.locals()ordir()results.Tests