Python 3.12+typingterm
typing.TypeAliasType
generics and parameters
The type of objects created by the `type X = ...` statement (type aliases; 3.12+).
Syntax
type X = int # X — экземпляр TypeAliasType
Instances usually come from the type X = ... statement (PEP 695); the TypeAliasType(name, value) constructor arrived in the same 3.12 and is for creating an alias at run time; versions before 3.12 take it from typing_extensions. With the type statement the right-hand side is evaluated lazily — only when you access X.__value__ — so an alias can reference names not yet defined, or itself for recursive types, without quoting them as strings.
Examples
from typing import TypeAliasType
type MyInt = int
print(isinstance(MyInt, TypeAliasType)) # → True
print(MyInt.__name__) # → MyInt
print(MyInt.__value__) # → <class 'int'>
print(MyInt is int) # → False
type Vector = list[float]
print(Vector.__value__) # → list[float]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy