Type alias in Python (2)
Published: (January 10, 2026 at 04:34 AM EST)
2 min read
Source: Dev.to
Source: Dev.to
Memo
- My post 1 – explains type alias (1).
- My post 3 – explains type alias (3).
- My post 4 – explains type alias (4).
- My post 5 – explains type alias (5).
Bound & Constraints
Type statements (numeric example)
''' Type statement '''
type TA1[T=float] = None
type TA2[T:float=float] = None # Bound
type TA3[T:(float, str)=float] = None # Constraints
# type TA4[T:(float, str)=(float, str)] = None # Constraints # Error
''' Type statement '''
TypeAliasType (numeric example)
''' TypeAliasType '''
# from typing import TypeVar, TypeAliasType
# T1 = TypeVar('T1', default=float)
# T2 = TypeVar('T2', bound=float, default=float) # Bound
# T3 = TypeVar('T3', (float, str), default=float) # Constraints
# T4 = TypeVar('T4', (float, str), default=(float, str)) # Constraints # Error
# Error for mypy
# TA1 = TypeAliasType('TA1', value=T1 | None, type_params=(T1,))
# TA2 = TypeAliasType('TA2', value=T2 | None, type_params=(T2,))
# TA3 = TypeAliasType('TA3', value=T3 | None, type_params=(T3,))
''' TypeAliasType '''
Variable assignments (numeric example)
x1: TA1[object] = None # No error
x2: TA1[complex] = None # No error
x3: TA1[float] = None # No error
# x3: TA1 = None # No error
x4: TA1[int] = None # No error
x5: TA1[bool] = None # No error
x6: TA1[str] = None # No error
y1: TA2[object] = None # Error
y2: TA2[complex] = None # Error
y3: TA2[float] = None # No error
# y3: TA2 = None # No error
y4: TA2[int] = None # No error
y5: TA2[bool] = None # No error
y6: TA2[str] = None # Error
z1: TA3[object] = None # Error
z2: TA3[complex] = None # Error
z3: TA3[float] = None # No error
# z3: TA3 = None # No error
z4: TA3[int] = None # Error
z5: TA3[bool] = Error
z6: TA3[str] = None # No error
Class hierarchy (object‑oriented example)
class A: ...
class B(A): ...
class C(B): ...
class D(C): ...
class E(D): ...
''' Type statement '''
type TA1[T=C] = None
type TA2[T:C=C] = None # Bound
type TA3[T:(C, str)=C] = None # Constraints
# type TA4[T:(C, str)=(C, str)] = None # Constraints # Error
''' Type statement '''
TypeAliasType (object‑oriented example)
''' TypeAliasType '''
# from typing import TypeVar, TypeAliasType
# T1 = TypeVar('T1', default=C)
# T2 = TypeVar('T2', bound=C, default=C) # Bound
# T3 = TypeVar('T3', C, str, default=C) # Constraints
# T4 = TypeVar('T4', C, str, default=(C, str)) # Constraints # Error