3.10. Typing Alias

  • Since Python 3.12 you can use type soft-keyword to define type aliases

3.10.1. The old way

>>> def add(a: int|float, b: int|float) -> int|float:
...     return a + b

3.10.2. Type Keyword

  • definition will stay until the end of the scope (module, class, function)

You can use type soft-keyword to define type aliases

>>> type number = int|float
>>>
>>> def add(a: number, b: number) -> number:
...     return a + b

3.10.3. Function Type Alias

  • Defined in PEP-...

  • Similar to type soft-keyword

  • It is only available during function definition

But, also there is a new syntax for defining type aliases available during function definition

>>> def add[number: (int,float)](a: number, b: number) -> number:
...     return a + b