Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

Python Type Alias Statements

Learn Python's type statement for creating type aliases, using aliases in annotations, and understanding their lazy evaluation without confusing them with the type() function.

Levels: Intermediate ~15 min read

Intermediate

Python can give a descriptive name to a type expression with the type statement. A type alias is a named description of an existing type expression; it does not create a new runtime class.

Create a type alias

The basic form is type Point = tuple[float, float]. The name on the left becomes the alias, and the expression on the right describes the aliased type.

python basic.py
1
2
3
4
type Point = tuple[float, float]

origin: Point = (0.0, 0.0)
print(origin)

The alias gives a reusable name to the type expression.

Use an alias in an annotation

A type alias can be used anywhere an annotation expression can be used. This keeps function signatures shorter and gives related values a shared vocabulary.

python annotation.py
1
2
3
4
5
6
7
type Coordinates = tuple[float, float]

def distance_from_origin(point: Coordinates) -> float:
    x, y = point
    return (x * x + y * y) ** 0.5

print(distance_from_origin((3.0, 4.0)))

The alias is used as the annotation for a function parameter; the function still behaves like ordinary Python code.

An alias can name a larger type expression

The right-hand side can be a type expression built from other types. Aliases are especially useful when the same long expression would otherwise be repeated.

python composed.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
type UserId = int
type UserRecord = tuple[UserId, str]
type UserTable = dict[UserId, UserRecord]

users: UserTable = {
    7: (7, "Ada"),
    8: (8, "Grace"),
}

print(users[7])

Aliases can be composed to make a larger annotation easier to read.

The alias is a TypeAliasType object

Executing a type statement creates a type-alias object. Its value can be inspected through the alias object's metadata, including its name and underlying type expression.

python alias-object.py
1
2
3
4
5
type Point = tuple[float, float]

print(Point.__name__)
print(type(Point).__name__)
print(Point.__value__)

The alias has its own name and exposes the aliased type expression through __value__.

Type aliases are evaluated lazily

The value of a type alias is evaluated lazily in its annotation scope. This means the referenced type expression does not have to be evaluated at the moment the alias statement runs.

python lazy.py
1
2
3
4
type FutureName = LaterName

LaterName = str
print(FutureName.__value__)

The later binding can exist before the alias value is requested because the alias value is evaluated lazily.

Type aliases do not enforce runtime types

A type alias is not a runtime validation rule. Using an alias in an annotation does not make Python automatically reject values of another type; annotations remain descriptive metadata unless some other tool or explicit code performs checking.

python no-enforcement.py
1
2
3
4
5
6
type UserId = int

def show_user(user_id: UserId):
    print(user_id)

show_user("guest")

The annotation uses the alias, but the interpreter does not automatically enforce it at the call site.

Do not confuse type with type()

The word type in a type-alias statement is a soft keyword. It does not replace the built-in type() function, which still asks about an object's runtime type.

python type-vs-type-function.py
1
2
3
4
5
type UserId = int

value = 42
print(type(value).__name__)
print(UserId.__name__)

type(value) inspects a runtime object; the type statement creates a named type alias.

Key idea

The type statement gives an existing type expression a reusable name. It creates a type-alias object, evaluates the alias value lazily, and improves the readability of annotations without creating a new runtime class or enforcing types by itself.

Was this page helpful?

Mark this learning resource complete to track your learning progress.