Keywords and Control Flow
type — Modern type alias statement
Declare a type alias using the contextual type soft keyword introduced in Python 3.12. This creates a typing.TypeAliasType object; its value is evaluated lazily in an annotation scope when __value__ is accessed. A forward name can therefore be defined after the alias declaration. Generic aliases declare their own type parameters. The alias does not validate runtime values and is not interchangeable with the built-in type() function or with every runtime class operation.
Formal Syntax & Structure
python syntaxpython
type Alias = type_expressiontype Alias[T] = type_expressionWorking Example
Example Snippetpython
type Coordinates = tuple[int, int]type Later = Itemclass Item: pass
type Box[T] = list[T]print(Coordinates.__name__)print(Later.__value__ is Item)print(len(Box.__type_params__))