Python 3.12+astterm
ast.TypeAlias
statements
Node for the type X = … statement (Python 3.12): name is a Name with Store, type_params holds type parameters, value is the right-hand side.
Syntax
ast.TypeAlias(name, type_params, value)
type is a soft keyword: type = 5 is still a plain Assign, and TypeAlias appears only for the type Name = … construct. Unlike Assign the value is evaluated lazily when __value__ is accessed, so names not yet defined are allowed in value. A generic alias type Pair[T] = … puts T into type_params as a TypeVar whose name is a string, not a Name.
Examples
import ast
a = ast.parse('type Pair[T] = tuple[T, T]').body[0]
print(type(a).__name__, a.name.id, type(a.name.ctx).__name__) # → TypeAlias Pair Store
print(type(a.type_params[0]).__name__, a.type_params[0].name) # → TypeVar T
print(type(ast.parse('type = 5').body[0]).__name__) # → Assign
See also
Open in the full glossarydocs.python.orgReport an inaccuracy