ID EN
Typing

TypeAlias

Python 3.11

Special annotation for explicitly declaring a type alias.

Syntax

PYTHON
typing.TypeAlias

Examples

Example 1
PYTHON
from typing import TypeAlias

Factors: TypeAlias = list[int]
Example 2
PYTHON
from typing import Generic, TypeAlias, TypeVar

T = TypeVar("T")

 "Box" does not exist yet,
 so we have to use quotes for the forward reference.
 Using ``TypeAlias`` tells the type checker that this is a type alias declaration,
 not a variable assignment to a string.
BoxOfStrings: TypeAlias = "Box[str]"

class Box(Generic[T]):
    @classmethod
    def make_box_of_strings(cls) -> BoxOfStrings: ...

See Also

type alias