This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rohanpadhye
Recipients rohanpadhye
Date 2018-10-09.01:58:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1539050318.57.0.545547206417.issue34939@psf.upfronthosting.co.za>
In-reply-to
Content
The following code when run as a script file gives syntax error:

```
def set_x():
    global x
    x = 1

x:int = 0   # SyntaxError: annotated name 'x' can't be global
```

PEP 526 does not seem to forbid this. The error message "annotated name [...] can't be global" is usually seen when using the `global x` declaration *in the same scope* as an annotated assignment. In the above case, the annotated assignment is outside the function scope, yet Python 3.7 gives a syntax error.


Is this a bug in CPython? Or should the PEP 526 document say something about forward references?

Interestingly, if the above program is run in interactive mode, there is no syntax error. 

In interactive mode:
```
>>> def set_x():
...     global x
...     x = 1
... 
>>> x:int = 0
>>> set_x()
>>> print(x)
1
```

Further, forward references work fine with `nonlocal`. For example, the following works fine both as a script file and in interactive mode:
```
def outer():
    def inner():
        nonlocal y
        y = 1
    y:int = 0
```

I don't see why a forward reference in `global` is a problem.
History
Date User Action Args
2018-10-09 01:58:38rohanpadhyesetrecipients: + rohanpadhye
2018-10-09 01:58:38rohanpadhyesetmessageid: <1539050318.57.0.545547206417.issue34939@psf.upfronthosting.co.za>
2018-10-09 01:58:38rohanpadhyelinkissue34939 messages
2018-10-09 01:58:37rohanpadhyecreate