New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make "global after use" a SyntaxError #72186
Comments
The documentation at https://docs.python.org/3/reference/simple_stmts.html says that: "Names listed in a global statement must not be used in the same code block textually preceding that global statement" But then later: "CPython implementation detail: The current implementation does not enforce the two restrictions, Code like this def f():
x = 1
global x gives SyntaxWarning for several releases, maybe it is time to make it a SyntaxError? |
I like fixing this. I suppose it'll be fixed for nonlocal too? |
Yes, nonlocal will be fixed too. This will be a relatively small patch, but I am a bit afraid there could be merge conflicts (the code affected could overlap with changes for PEP-526). Easiest way would be to simply make this a single patch with PEP-526 implementation (this will actually even slightly simplify the implementation). Alternatively we could fix this right after the implementation patch is applied. What do you prefer? |
I strongly prefer to have it as a separate patch (either before or |
OK, then I think after will be safer. Let us came back to this right after PEP-526. |
I am attaching the patch. Yury, I also added you here, just in case you have time for this. Btw, while working on this I have found that the second restriction: """ is not enforced at all, it does not give even a SyntaxWarning. |
Added few tests to the patch |
I'll commit this. |
New changeset 804b71d43c85 by Guido van Rossum in branch 'default': |
Thanks again Ivan! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: