classification
Title: Make "global after use" a SyntaxError
Type: behavior Stage:
Components: Documentation, Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: docs@python, gvanrossum, levkivskyi, python-dev, yselivanov
Priority: normal Keywords: patch

Created on 2016-09-07 14:08 by levkivskyi, last changed 2016-09-09 16:53 by gvanrossum. This issue is now closed.

Files
File name Uploaded Description Edit
patch-v1.diff levkivskyi, 2016-09-09 12:13 review
patch-v2.diff levkivskyi, 2016-09-09 14:18 review
Messages (10)
msg274813 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) Date: 2016-09-09 14:18
Added few tests to the patch
msg275332 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-09-09 16:25
I'll commit this.
msg275344 - (view) Author: Roundup Robot (python-dev) (Python triager) 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) * (Python committer) Date: 2016-09-09 16:53
Thanks again Ivan!
History
Date User Action Args
2016-09-09 16:53:54gvanrossumsetstatus: open -> closed
resolution: fixed
messages: + msg275346
2016-09-09 16:53:19python-devsetnosy: + python-dev
messages: + msg275344
2016-09-09 16:25:59gvanrossumsetassignee: docs@python -> gvanrossum
messages: + msg275332
2016-09-09 14:18:51levkivskyisetfiles: + patch-v2.diff

messages: + msg275318
2016-09-09 12:13:10levkivskyisetfiles: + patch-v1.diff

nosy: + yselivanov
messages: + msg275314

keywords: + patch
2016-09-07 21:11:37levkivskyisetmessages: + msg274895
2016-09-07 21:08:05gvanrossumsetmessages: + msg274894
2016-09-07 20:51:47levkivskyisetmessages: + msg274888
2016-09-07 16:58:56gvanrossumsetmessages: + msg274836
2016-09-07 14:08:38levkivskyicreate