Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A type alias is a user-defined identifier that serves as a static synonym for an existing type annotation. It is evaluated by static type checkers (such as mypy or pyright) as strictly equivalent to the underlying type, meaning it does not create a new distinct runtime class or subclass.

Syntax Evolution

The implementation of type aliases in Python has evolved significantly across versions.

Python 3.12+ (Modern Syntax)

Python 3.12 introduced the type soft keyword, which formally defines a type alias. This syntax creates an instance of typing.TypeAliasType at runtime and supports lazy evaluation, allowing for forward references without string literals.
type Point = tuple[float, float]
type NestedDict = dict[str, dict[str, int]]

Python 3.10 - 3.11 (TypeAlias)

To disambiguate type aliases from standard global variable assignments, Python 3.10 introduced the TypeAlias annotation in the typing module.
from typing import TypeAlias

Point: TypeAlias = tuple[float, float]
NestedDict: TypeAlias = dict[str, dict[str, int]]

Pre-Python 3.10 (Legacy Syntax)

Historically, type aliases were created using standard variable assignment. While still supported, this approach lacks explicit intent for static analyzers.
Point = tuple[float, float]
NestedDict = dict[str, dict[str, int]]

Generic Type Aliases

Type aliases support generics, allowing them to accept type parameters.

Python 3.12+ Generics

The type statement supports inline type parameters using square brackets, automatically scoping the type variables.
type Matrix[T] = list[list[T]]
type Result[V, E] = V | E

Pre-Python 3.12 Generics

Older versions require the explicit instantiation of typing.TypeVar objects.
from typing import TypeVar, TypeAlias

T = TypeVar('T')
V = TypeVar('V')
E = TypeVar('E')

Matrix: TypeAlias = list[list[T]]
Result: TypeAlias = V | E

Technical Characteristics

  • Equivalence: A type alias is structurally identical to its target. If type A = int, a type checker will accept an int wherever A is expected, and vice versa. It is not a nominal type.
  • Lazy Evaluation (3.12+): The right-hand side of a type statement is not evaluated when the module is loaded. It is evaluated only when the alias’s __value__ attribute is accessed, enabling recursive type definitions natively.
  • Introspection: In Python 3.12+, type aliases can be inspected at runtime via the __name__, __type_params__, and __value__ attributes of the TypeAliasType object.
Master Python with Deep Grasping Methodology!Learn More