Python Glossary Русский

Python 3.12+Annotations & typingconstruct

The type statement

syntax

type X = ... declares a type alias (Python 3.12+). The right-hand side is evaluated lazily, so it may refer to names that are not defined yet.

Syntax

type Имя = выражение
type Имя[T] = выражение

The main trap: type does not create the type itself but a TypeAliasType object — isinstance(x, Point) raises TypeError, and the type itself lives in Point.__value__. The right-hand side is evaluated lazily, on the first access to __value__, so a typo in it surfaces as NameError later, not on the type line. A plain assignment Point = tuple[float, float] gives an ordinary object without the laziness, yet isinstance rejects it too — a parameterized type cannot be used for instance checks; type is for explicit, recursive and generic aliases.

Examples

# Python 3.12+
type Point = tuple[float, float]
print(type(Point).__name__)  # → TypeAliasType
print(Point.__value__)  # → tuple[float, float]
isinstance((1.0, 2.0), Point)  # → TypeError
Pair = tuple[float, float]   # простое присваивание, без type
print(Pair)  # → tuple[float, float]
isinstance((1.0, 2.0), Pair)  # → TypeError — параметризованный тип проверке не годится
# Python 3.12+
type Later = Undefined
print(Later.__name__)  # → Later
print(Later.__value__)  # → NameError
# Python 3.12+
type Tree = list[Tree] | int
print(Tree.__value__ == (list[Tree] | int))  # → True

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy