3. Use the type statement for type aliases
In Python 3.12 and later, prefer the type statement for type aliases. It is clearer than assigning a type expression to a normal variable.
The dedicated syntax removes ambiguity between a type alias and an ordinary runtime assignment. That helps both readers and tooling distinguish definition-time type vocabulary from ordinary variables.
Note
Python 3.12+
3.1. Don’t do this
1from typing import TypeAlias
2
3UserId: TypeAlias = int
4ScoresByUser: TypeAlias = dict[UserId, list[int]]
3.2. Do this
1type UserId = int
2type ScoresByUser = dict[UserId, list[int]]