You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
An import of a name before its global or nonlocal declaration is not a SyntaxError #162598
The Reference says "A SyntaxError is raised if a variable is used or assigned to prior to its global declaration in the scope" (global), the same for nonlocal, and an import binds names "just as an assignment statement would (including global and nonlocal semantics)". An assignment before global x is rejected, an import is not:
The import then binds the global, and the same happens before nonlocal x and at module level. The check for Global_kind and Nonlocal_kind in symtable_visit_stmt (Python/symtable.c) tests DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT but not DEF_IMPORT. I found it while writing a Lean spec of Python.
Thank you for taking the time to open this issue. Python is a community-driven project, and we want it to be a welcoming place to learn and collaborate.
This issue appears to have been written with AI assistance. Please explain your issue in your own words and describe how you checked the information you provided. This helps us understand your issue and leaves more time to address other issues.
@Robertboy18 Your reproducers are very hard to read. PLease don't use a loop with compile/exec and newlines it's impossible to reproduce easily on our side and investigate.
Activity
bedevere-app commentedon Nov 25, 2026
Thank you for taking the time to open this issue. Python is a community-driven project, and we want it to be a welcoming place to learn and collaborate.
This issue appears to have been written with AI assistance. Please explain your issue in your own words and describe how you checked the information you provided. This helps us understand your issue and leaves more time to address other issues.
Please read our guidelines for using AI tools.
picnixz commentedon Nov 25, 2026
@Robertboy18 Your reproducers are very hard to read. PLease don't use a loop with compile/exec and newlines it's impossible to reproduce easily on our side and investigate.