msg274813 - (view) |
Author: Ivan Levkivskyi (levkivskyi) *  |
Date: 2016-09-07 14:08 |
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, but programs should not abuse this freedom, as future implementations may enforce them..." Code like this def f(): x = 1 global x gives SyntaxWarning for several releases, maybe it is time to make it a SyntaxError? |
|
|
msg274836 - (view) |
Author: Guido van Rossum (gvanrossum) *  |
Date: 2016-09-07 16:58 |
I like fixing this. I suppose it'll be fixed for nonlocal too? |
|
|
msg274888 - (view) |
Author: Ivan Levkivskyi (levkivskyi) *  |
Date: 2016-09-07 20:51 |
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? |
|
|
msg274894 - (view) |
Author: Guido van Rossum (gvanrossum) *  |
Date: 2016-09-07 21:08 |
I strongly prefer to have it as a separate patch (either before or after) to avoid the appearance of piggy-backing this in with a much larger change. |
|
|
msg274895 - (view) |
Author: Ivan Levkivskyi (levkivskyi) *  |
Date: 2016-09-07 21:11 |
OK, then I think after will be safer. Let us came back to this right after PEP 526. |
|
|
msg275314 - (view) |
Author: Ivan Levkivskyi (levkivskyi) *  |
Date: 2016-09-09 12:13 |
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: """ Names listed in a global statement must not be ... in a for loop control target, class definition, function definition, or import statement. """ is not enforced at all, it does not give even a SyntaxWarning. Is it something worth fixing? |
|
|
msg275318 - (view) |
Author: Ivan Levkivskyi (levkivskyi) *  |
Date: 2016-09-09 14:18 |
Added few tests to the patch |
|
|
msg275332 - (view) |
Author: Guido van Rossum (gvanrossum) *  |
Date: 2016-09-09 16:25 |
I'll commit this. |
|
|
msg275344 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2016-09-09 16:53 |
New changeset 804b71d43c85 by Guido van Rossum in branch 'default': Issue #27999: Make "global after use" a SyntaxError, and ditto for nonlocal. https://hg.python.org/cpython/rev/804b71d43c85 |
|
|
msg275346 - (view) |
Author: Guido van Rossum (gvanrossum) *  |
Date: 2016-09-09 16:53 |
Thanks again Ivan! |
|
|