Repository navigation
Conversation
… nonlocal declaration
|
Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate. Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors. This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness. Please read our guidelines for using AI tools. |
|
the problem is that an import binds a name in the same way as an assignment, but the check in instead of adding another flag to the check, i reused the existing to verify the change, i added two regression tests, one for global and one for nonlocal. both fail without the fix and pass with it. i also checked the same behavior with pypy and ran all eight related test modules successfully. |
Fixes #162598.
CPython currently accepts this function even though
import os.pathbindsosbefore its global declaration:The symbol table already records the name bound by an import, including an alias. Use its existing
DEF_BOUNDmask when checkingglobalandnonlocaldeclarations so an earlier import receives the same diagnostic as an earlier assignment. Code that relied on the previous behavior can move the declaration above the import.The two regression tests cover a dotted import before
globaland an aliasedfromimport beforenonlocal. Both failed on unmodified CPython and passed with the fix. PyPy also rejects both orderings. All eight modules passed locally with:./python -m test -j2 test_syntax test_global test_scope test_symtable test_compile test_grammar test_import test_lazy_importOpenAI Codex helped reproduce the issue, write the patch and tests, and run the comparisons.
importof a name before itsglobalornonlocaldeclaration is not aSyntaxError#162598