DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

Type alias in Python (1)

Buy Me a Coffee

*Memo:

  • My post explains type alias (2).
  • My post explains type alias (3).
  • My post explains type alias (4).
  • My post explains type alias (5).
  • My post explains type alias (6).
  • My post explains type hint (1).
  • My post explains the memo for type hints-related posts.

A type alias can be created as shown below:

*Memo:

  • A type alias:
    • is the existing type given a concise name(identifier) to so it's not a new type.
    • can be created with a type(non-union or union type) by the new syntax a type statement and the old syntax an assignment statement and TypeAlias:
      • A type can also contain type aliases and NewType's new types.
      • A type statement:
        • uses type keyword.
        • can be used from Python 3.12.
        • is based on TypeAliasType which can also be used to create a type alias from Python 3.12 but don't use it because a type statement is easier:
      • An assignment statement can be used from Python 3.5 but don't use it because it isn't distinct without type keyword and cannot create a generic type alias.
      • TypeAlias can be used from Python 3.10 but don't use it because it's deprecated.
    • cannot be extended by a class:
      • The type alias created with an assignment statement or TypeAlias doesn't get error accoding to the issue.
    • can be generic, which is called a generic type alias:
      • A generic type alias cannot be created by an assignment statement.
  • For TypeAliasType:
    • The 1st parameter is name(Required-KP-Type:str):
      • It's an identifier.
      • It must be the same name as its variable.
      • In convention, PascalCase is used like MyTypeAlias.
    • The 2nd parameter is value(Required-KP-Type:Type):
      • It's a type.
    • The 3rd parameter is type_params(Optional-KO-Type:tuple(type parameters)):
  • The syntax of a generic type alias is:
    • type name[parameter, ...] = non-union/union type(Required):
      • parameter can be used within non-union/union type.
      • My post explains the new syntax of generics name[parameter, ...] and the old syntax, and their relates.
  • Unpacking a type alias of a tuple is possible with * from Python 3.14:
    • The old way with Unpack is still possible.
  • The doc explains a type alias.

Type alias:

from typing import reveal_type

''' Type statement '''
type TA1 = int | str | list[int]
                 # Runtime       # Unruntime
reveal_type(TA1) # TypeAliasType # typing.TypeAliasType
''' Type statement '''

''' TypeAliasType '''
# from typing import TypeAliasType

# TA1 = TypeAliasType('TA1', value=int | str | list[int]) # Error for mypy
#                  # Runtime       # Unruntime
# reveal_type(TA1) # TypeAliasType # typing.TypeAliasType
''' TypeAliasType '''

''' Assignment statement '''
# TA1 = int | str | list[int]
#                  # Runtime # Unruntime
# reveal_type(TA1) # Union   # types.UnionType[...]
''' Assignment statement '''

''' TypeAlias '''
# from typing import TypeAlias

# TA1: TypeAlias = int | str | list[int]
#                  # Runtime # Unruntime
# reveal_type(TA1) # Union   # types.UnionType[...]
''' TypeAlias '''

v1: TA1 = 100         # No error
v1 = 'Hello'          # No error
v1 = [0, 1, 2]        # No error
v1 = ('A', 0, 'B', 1) # Error
v1 = None             # Error

type TA2 = tuple[int, str, int]
type TA3 = TA1 | tuple[str, *TA2] | None

v2: TA3 = 100         # No error
v2 = 'Hello'          # No error
v2 = [0, 1, 2]        # No error
v2 = ('A', 0, 'B', 1) # No error
v2 = None             # No error
Enter fullscreen mode Exit fullscreen mode
from typing import NewType

NT = NewType('NT', int)

type TA = NT # No error
Enter fullscreen mode Exit fullscreen mode
type TA = int

class Cls(TA): pass # Error
Enter fullscreen mode Exit fullscreen mode

Generic type alias:

<TypeVar base>:

from typing import reveal_type

''' Type statement '''
type TA1[T1, T2=int] = tuple[T1, T2, T1, T2] | T1 | bool
                 # Runtime       # Unruntime
reveal_type(TA1) # TypeAliasType # typing.TypeAliasType
''' Type statement '''

''' TypeAliasType '''
# from typing import TypeVar, TypeAliasType

# T1 = TypeVar('T1')
# T2 = TypeVar('T2', default=int)

# TA1 = TypeAliasType('TA1', value=tuple[T1, T2, T1, T2] | T1 | bool,
#                     type_params=(T1, T2)) # Error for mypy
#                  # Runtime       # Unruntime
# reveal_type(TA1) # TypeAliasType # typing.TypeAliasType
''' TypeAliasType '''

''' Assignment statement '''
# TA1[T1, T2=int] = tuple[T1, T2, T1, T2] | T1 | bool # Error
''' Assignment statement '''

''' TypeAlias '''
# from typing import TypeVar, TypeAlias

# T1 = TypeVar('T1')
# T2 = TypeVar('T2', default=int)

# TA1: TypeAlias = tuple[T1, T2, T1, T2] | T1 | bool
#                  # Runtime # Unruntime
# reveal_type(TA1) # Union   # types.UnionType[...]
''' TypeAlias '''

v1_1: TA1[str] = ('A', 0, 'B', 1)        # No error
# v1_1: TA1[str, int] = ('A', 0, 'B', 1) # No error
# v1_1: TA1[] = ('A', 0, 'B', 1)         # Error
# v1_1: TA1 = ('A', 0, 'B', 1)           # Error
v1_1 = 'Hello'                           # No error
v1_1 = 100                               # Error
v1_1 = True                              # No error
v1_1 = [0.0, 1.0, 2.0]                   # Error
v1_1 = None                              # Error

v1_2: TA1[int, str] = (0, 'A', 1, 'B') # No error
v1_2 = 'Hello'                         # Error
v1_2 = 100                             # No error
v1_2 = True                            # No error
v1_2 = [0.0, 1.0, 2.0]                 # Error
v1_2 = None                            # Error

v1_3: TA1[str|int, str|int] = ('A', 'B', 0, 1) # No error
v1_3 = 'Hello'                                 # No error
v1_3 = 100                                     # No error
v1_3 = True                                    # No error
v1_3 = [0.0, 1.0, 2.0]                         # Error
v1_3 = None                                    # Error

# A type alias can be created with a type alias.
type TA2[T1=str, T2=int, T3=float] = TA1[T1, T2] | list[T3] | None

v2_1: TA2 = ('A', 0, 'B', 1)                    # No error
# v2_1: TA2[str, int, float] = ('A', 0, 'B', 1) # No error
# v2_1: TA2[] = ('A', 0, 'B', 1)                # Error
v2_1 = 'Hello'                                  # No error
v2_1 = 100                                      # Error
v2_1 = True                                     # No error
v2_1 = [0.0, 1.0, 2.0]                          # No error
v2_1 = None                                     # No error

v2_2: TA2[int, str, float] = (0, 'A', 1, 'B') # No error
v2_2 = 'Hello'                                # Error
v2_2 = 100                                    # No error
v2_2 = True                                   # No error
v2_2 = [0.0, 1.0, 2.0]                        # No error
v2_2 = None                                   # No error

v2_3: TA2[int|str, int|str|float] = ('A', 'B', 0, 1) # No error
v2_3 = 'Hello'                                       # No error
v2_3 = 100                                           # No error
v2_3 = True                                          # No error
v2_3 = [0.0, 1.0, 2.0]                               # No error
v2_3 = None                                          # No error
Enter fullscreen mode Exit fullscreen mode
# Run without `--strict`
# No type arguments make the type parameters without default types `Any`.

from typing import reveal_type

type TA[T1, T2=int] = tuple[T1, T2, T1, T2] | T1 | bool

v: TA = ('A', 0, 'B', 1)
reveal_type(v)
# tuple[Any, builtins.int, Any, builtins.int] | Any | builtins.bool
Enter fullscreen mode Exit fullscreen mode
# A type alias can be created with a `NewType`'s new type.

from typing import NewType

NT = NewType('NT', int)

type TA[T] = tuple[NT, T] # No error
Enter fullscreen mode Exit fullscreen mode
# A type alias cannot be extended by a class.

type TA[T] = T

class Cls(TA[int]): pass # Error
Enter fullscreen mode Exit fullscreen mode

Top comments (0)