Programming Search⌘K

Featurecore

Annotated assignment

Also called variable annotation, PEP 526, Python variable type hints, __annotations__, declare a variable type in Python.

x: int = 0 adds an annotation to an assignment. The value part is optional, and Python does not check the annotation. A class or module keeps the annotations of its simple names in __annotations__.

Language
Python

Synopsis

class Point:
    x: int = 0
    y: int
    label: str = "origin"

print(Point.__annotations__)
print(Point.x, hasattr(Point, "y"))
Output
{'x': <class 'int'>, 'y': <class 'int'>, 'label': <class 'str'>}
0 False

y: int has no value. It adds an annotation, but it does not create the attribute y.

Semantics

r1. "Annotation assignment is the combination, in a single statement, of a variable or attribute annotation and an optional assignment statement". "The difference from normal Assignment statements is that only a single target is allowed."1 Example: class-fields.

r2. "The assignment target is considered “simple” if it consists of a single name that is not enclosed in parentheses." "For simple assignment targets, if in class or module scope, the annotations are gathered in a lazily evaluated annotation scope. The annotations can be evaluated using the annotations attribute of a class or module".1 Example: class-fields.

r3. "If a name is annotated in a function scope, then this name is local for that scope. Annotations are never evaluated and stored in function scopes."1 PEP 526 shows that an annotation with no value makes the name local, so a use before an assignment "raises UnboundLocalError".2 Examples: local-annotation, annotation-makes-local.

r4. "If the right hand side is present, an annotated assignment performs the actual assignment as if there was no annotation present."1 Python does not check the value against the annotation. "Variable annotations are usually used for type hints", and type hints "are not enforced by Python".3 Example: not-checked.

r5. "If the assignment target is not simple (an attribute, subscript node, or parenthesized name), the annotation is never evaluated."1 Example: attribute-target.

r6. PEP 526 says that the syntax "does not allow one to annotate the types of variables when tuple unpacking is used". Annotate each name first, and then unpack.2 Example: no-unpacking.

Examples

def f():
    x: NonexistentName = 1
    count: int
    return x

print(f())
Output
1

The annotation NonexistentName is not defined, but Python does not evaluate annotations in a function, so the call works (rule r3).

total: int = "not a number"
print(total, type(total).__name__)
Output
not a number str
class Box:
    pass

box = Box()
box.size: int = 3
box.color: str
print(box.size, hasattr(box, "color"))
Output
3 False

box.color: str has no value, so it assigns nothing (rule r5).

This code raises SyntaxError, on purpose.

a, b: int = 1, 2

Pitfalls

An annotation with no value makes the name local in a function, even when the function only reads the global name:

This code raises UnboundLocalError, on purpose.

a = 1

def f():
    a: int
    print(a)

f()

PEP 526 explains this case: the code acts "as if the code were" a function with an if False: a = 0 before the use.2 Remove the annotation, or annotate a different name.

An annotation is not a check. A wrong value runs with no error (example not-checked). A static type checker can find the mistake before the program runs.3

Comparison

In Go, "A variable declaration creates one or more variables, binds corresponding identifiers to them, and gives each a type and an initial value". With no value, "each variable is initialized to its zero value".4 And "If a type is present, each variable is given that type."4 A Python annotation is only information for tools. With no value, the Python name stays unbound (rules r3 and r4).

References

  1. The Python Language Reference, Python 3.15, 7.2.2. Annotated assignment statements
  2. PEP 526 - Syntax for Variable Annotations, Abstract; Global and local variable annotations
  3. Glossary, Python 3.15 documentation, variable annotation; type hint
  4. The Go Programming Language Specification, Variable declarations

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source