TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
type statement is a soft keyword introduced in Python 3.12 (PEP 695) used to declare type aliases. It provides a dedicated, native syntax for defining both generic and non-generic type aliases, replacing the older typing.TypeAlias assignment method while introducing lazy evaluation, implicit variance inference, and strict scoping.
Syntax
The statement follows this general grammar:Technical Mechanics
1. Underlying Object (typing.TypeAliasType)
When the Python interpreter executes a type statement, it does not simply assign the right-hand side to the left-hand side. Instead, it creates an instance of typing.TypeAliasType and binds it to the AliasName.
TypeExpression on the right-hand side is lazily evaluated. The Python compiler wraps the expression in an implicit function. This allows the alias to reference names that have not yet been defined (forward references) without raising a NameError at runtime, eliminating the need for string literals.
The actual value is only evaluated when explicitly requested via the __value__ attribute.
type statement introduces an annotation scope. Type parameters defined within the square brackets [...] are implicitly created and are strictly scoped to the TypeExpression. They do not leak into the surrounding module namespace.
typing.TypeVar instantiation which required explicit covariant=True or contravariant=True flags, type parameters declared via the type statement automatically infer their variance based on how they are used within the TypeExpression.
Generic Type Parameter Syntax
The square bracket syntax supports standardTypeVar declarations, bound types, constrained types, TypeVarTuple (variadic generics), and ParamSpec (callable signatures).
- Standard Type Variables:
-
Bound Type Variables (using
:with a single type): Restricts the type variable to the specified type and its subclasses.
-
Constrained Type Variables (using
:with a tuple of types): Restricts the type variable to exactly the specified types (no subclasses).
- TypeVarTuple (using
*):
- ParamSpec (using
**):
Runtime Introspection Attributes
Instances created by thetype statement expose specific dunder attributes for runtime introspection:
__name__: The string name of the type alias.__value__: The evaluated type expression (triggers lazy evaluation).__type_params__: A tuple of the implicit type parameter objects (e.g.,TypeVarinstances) declared in the generic signature.
Master Python with Deep Grasping Methodology!Learn More





