26

Running

def function():
    global global_values
    global_values: str = []

gives

SyntaxError: annotated name 'global_values' can't be global

Is there any reason for this?

2
  • 1
    I got the same error when using global and type annotation INSIDE a function. Putting the type annotation in the global space solved it. PS: Executing the lines of code that you proposed in python 3.8.8 does not generate any error in my setup.
    – Mike
    Commented Nov 7, 2021 at 17:47
  • @Mike I'm getting this SyntaxError, while having the type annotation in global space and the global statement within a function. Also within Python 3.10 I get the same error. The OP should have put the statement within a function (as it doesn't make sense to use a global keyword in the global space, i think)
    – PythoNic
    Commented Oct 17, 2022 at 16:42

3 Answers 3

20

This has been explained in PEP-526:

It is illegal to attempt to annotate variables subject to global or nonlocal in the same function scope:

def f():
    global x: int  # SyntaxError

def g():
    x: int  # Also a SyntaxError
    global x

The reason is that global and nonlocal don't own variables; therefore, the type annotations belong in the scope owning the variable.

The solution is to annotate the variable outside the function (i.e., in the global namespace), and remove type annotations inside the function's scope.

3
  • 4
    What does that mean? So how do you type annotate a global??
    – uchuugaka
    Commented Nov 26, 2021 at 3:05
  • 1
    @uchuugaka It means that the only place you can annotate a global variable is in its own scope, IE in the global scope. Commented Nov 9, 2022 at 10:01
  • So we can just write x: int before calling the mehods. It's probably best to annotate the variable there anyway when we declare it. Commented Jan 9 at 15:59
9

So, as is often the case with very old language features, this is confusing. global in python is a statement that declares a name to be matching a key in the globals() dictionary-like container. If the key is not already in the globals() dictionary-like container, it will be added. It will default to a value of None at that time. If a variable is assigned in the global scope (and defined at that time), then the variable's name is added to the globals() dictionary-like container and the value is set to the value for the key matching the name in the globals() dictionary-like container.

So, the global keyword is a statement that adds a name to that globals() dictionary-like container. Unfortunately, you cannot do assignments to the variable in the same line as using global to add that variable's name to globals() dictionary-like container.

Because of this sadness, adding a name to globals() dictionary-like container will IMPLICITLY make it an Optional type in Python's typing world.

I still think this is a bug mentally. It is not obvious. It is not clear. It is not really sensible. Yet, there it is.

The right thing to do would be to have the ability to immediately assign a value to a global at the same time it is made global and thus have a non-Optional global type, and also remove the non-obvious, confusing troublesome edge case this is. Typing is great and would be lovely to have more of it in Python. It helps scale, it helps remove logical errors and ambiguities that cause difficult bugs. It highlights when you have situations where you have not accounted for a variable possibly being a type wholly incompatible with the code written.

1
  • Is it an edge case? Thanks for your nice answer. Commented Jan 16, 2022 at 23:57
1

The given explanation of the accepted answer actually makes zero sense whatsoever, moreover it seems annotating global variables been the consensus as should be allowed in this discussion: https://bugs.python.org/issue34939, however as of yet - Python 3.8 - it hasn't been implemented.

The problem must be in the implementation rather than a logical one.

3
  • I disagree with the statement that the accepted answer makes no sense - it does and it answers the OP use case. Nonetheless, the link to the bug you mention is in fact exactly what I was looking for, because I have code that ran in Python 3.9.7 but showed the exact bug with 3.7.8. But the circumstances leading up to that bug are different from the OPs question.
    – pogojotz
    Commented Sep 28, 2021 at 9:50
  • 3
    The accepted answer is a quote but not an explanation of the error.
    – uchuugaka
    Commented Nov 26, 2021 at 3:07
  • @uchuugaka true, the accepted answer quotes an explanation, and my answer says the given explanation quoted there makes no logical sense. I've also given a link to people working on this "bug" that a global variable can't be annotated.
    – j4hangir
    Commented Nov 28, 2021 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.