Sub topic 15
Forward references and postponed evaluation
This chapter assumes you can write basic annotations, use X | None, and define classes. It covers what happens when an annotation names a type that does not exist yet.
The problem: names that are not defined yet
Python normally evaluates an annotation when it executes the def or assignment that contains it. A class name is not bound until its body finishes. An annotation inside the class that names the class therefore fails:
class Node:
def __init__(self, value: int, next: Node | None = None) -> None:
self.value = value
self.next = nextOn Python 3.13 and earlier, this raises NameError: name 'Node' is not defined. A reference to a name that is defined later is called a forward reference. Python 3.14 evaluates annotations lazily, so this exact code works there. Code that must run on older versions still needs one of the fixes below.
Fix 1: quote the annotation
A string annotation is not evaluated when the function is defined. The type checker reads it later.
class Node:
def __init__(self, value: int, next: "Node | None" = None) -> None:
self.value = value
self.next = next
print(Node.__init__.__annotations__["next"])Quote the whole expression. Writing "Node" | None fails at runtime on older versions, because a str does not support | with None.
Fix 2: postpone evaluation for the whole module
Add from __future__ import annotations as the first statement in the file. Python then stores every annotation in the module as a string and never evaluates it at definition time.
from __future__ import annotations
class Node:
def __init__(self, value: int, next: Node | None = None) -> None:
self.value = value
self.next = next
print(Node.__init__.__annotations__)The output shows plain strings such as 'Node | None'. The | syntax in an annotation is also safe on older Python versions, because the expression is never evaluated.
Runtime tools must resolve the strings
Postponed annotations are strings, so code that reads them at runtime has to turn them back into types. Use typing.get_type_hints:
from __future__ import annotations
from typing import get_type_hints
class Node:
def __init__(self, value: int, next: Node | None = None) -> None:
self.value = value
self.next = next
print(get_type_hints(Node.__init__))get_type_hints looks names up in the function's module globals. It raises NameError if a name is missing. Names imported only under if TYPE_CHECKING: are a common cause.
Common errors
- Quoting only part of an expression, for example
"Node" | None. - Using a quoted name in
isinstance(x, "Node"). Strings work only in annotations, not in runtime expressions. - Calling
get_type_hintswhen a name is imported only for type checking. - Expecting
__annotations__to hold real types after the future import. It holds strings.
Exercise
A module starts with from __future__ import annotations. What does Node.__init__.__annotations__["next"] contain for the parameter next: Node | None, and which function converts it to a real type?
Show answer
It contains the string 'Node | None'. typing.get_type_hints(Node.__init__) evaluates it and returns the real types.