Python Type Aliases (PEP 695) — ELI5
Imagine you bake a special cake every birthday. Instead of saying “the three-layer chocolate cake with raspberry filling and cream cheese frosting” every time, you just call it “Birthday Cake.” Everyone in your family knows what you mean.
Type aliases in Python work exactly like that. They give a short name to a long, complicated type description.
Before Python 3.12, creating a nickname for a type was awkward. You had to import special tools and write a bunch of setup code:
from typing import TypeVar, TypeAlias
T = TypeVar("T")
UserList: TypeAlias = list[dict[str, str]]
It worked, but it felt like filling out a form just to pick a nickname.
Python 3.12 introduced PEP 695, and now you just write:
type UserList = list[dict[str, str]]
That’s it. One line. The word type is the magic word that tells Python “I’m creating a nickname.”
What about generic types? Sometimes you want a nickname that works with different kinds of things — like “a box that holds anything.” The old way needed even more setup:
T = TypeVar("T")
class Box(Generic[T]):
value: T
The new way:
class Box[T]:
value: T
The [T] right after the name means “this works with any type.” No imports, no setup, no fuss.
Why does this matter? Because type hints help tools find bugs in your code before you run it. The easier they are to write, the more people use them, and the fewer bugs slip through.
The one thing to remember: PEP 695 turned type aliases from a bureaucratic form into a simple one-liner — making Python’s type system easier for everyone.
See Also
- Python 310 New Features Python 3.10 gave programmers a shape-sorting machine, friendlier error messages, and cleaner ways to say 'this or that' in type hints.
- Python 311 New Features Python 3.11 made everything faster, error messages smarter, and let you catch several mistakes at once instead of stopping at the first one.
- Python 312 New Features Python 3.12 made type hints shorter, f-strings more powerful, and started preparing Python's engine for a world without the GIL.
- Python 313 New Features Python 3.13 finally lets multiple tasks run at the same time for real, added a speed booster engine, and gave the interactive prompt a colourful makeover.
- Python Exception Groups Python's ExceptionGroup is like getting one report card that lists every mistake at once instead of stopping at the first one.