Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

gh-162598: Reject imports that bind a name before its global or nonlocal declaration - #162844

Open
saggl wants to merge 1 commit into
python:mainfrom
saggl:gh-162598-import-before-scope-declaration
Open

saggl wants to merge 1 commit into
python:mainfrom
saggl:gh-162598-import-before-scope-declaration

Conversation

@saggl

@saggl saggl commented Nov 29, 2026 •

Copy link
Copy Markdown

Fixes #162598.

CPython currently accepts this function even though import os.path binds os before its global declaration:

def f():
    import os.path
    global os

The symbol table already records the name bound by an import, including an alias. Use its existing DEF_BOUND mask when checking global and nonlocal declarations 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 global and an aliased from import before nonlocal. 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_import

OpenAI Codex helped reproduce the issue, write the patch and tests, and run the comparisons.

@python-cla-bot

python-cla-bot Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@picnixz picnixz added the AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor label Nov 30, 2026
@bedevere-app

bedevere-app Bot commented Nov 30, 2026

Copy link
Copy Markdown

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.

@saggl

saggl commented Nov 30, 2026

Copy link
Copy Markdown
Author

the problem is that an import binds a name in the same way as an assignment, but the check in symtable.c does not consider DEF_IMPORT when checking global and nonlocal declarations.

instead of adding another flag to the check, i reused the existing DEF_BOUND mask which already includes all relevant flags.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor awaiting review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An import of a name before its global or nonlocal declaration is not a SyntaxError

2 participants