*Memo:
- My post explains type alias (1).
- My post explains type alias (2).
- My post explains type alias (3).
- My post explains type alias (4).
- My post explains type alias (6).
<ParamSpec base>:
from collections.abc import Callable from typing import reveal_type ''' Type statement ''' type TA1[**P1, **P2=[None]] = Callable[P1, Callable[P2, None]] # Runtime # Unruntime reveal_type(TA1) # TypeAliasType # typing.TypeAliasType ''' Type statement ''' ''' TypeAliasType ''' # from typing import ParamSpec, TypeAliasType # P1 = ParamSpec('P1') # P2 = ParamSpec('P2', default=[None]) # TA1 = TypeAliasType('TA1', value=Callable[P1, Callable[P2, None]], # type_params=(P1, P2)) # Error for mypy # # Runtime # Unruntime # reveal_type(TA1) # TypeAliasType # typing.TypeAliasType ''' TypeAliasType ''' ''' Assignment statement ''' # TA1[**P1, **P2=[None]] = Callable[P1, Callable[P2, None]] # Error ''' Assignment statement ''' ''' TypeAlias ''' # from typing import ParamSpec, TypeAlias # P1 = ParamSpec('P1') # P2 = ParamSpec('P2', default=[None]) # TA1: TypeAlias = Callable[P1, Callable[P2, None]] # # Runtime # Unruntime # reveal_type(TA1) # _CallableGenericAlias # typing._SpecialForm ''' TypeAlias ''' lam = lambda x, y: lambda z: None v1_1: TA1[[int, str]] = lam # v1_1: TA1[[int, str], [None]] = lam # v1_1: TA1[[], []] = lam # Error # v1_1: TA1[] = lam # Error # v1_1: TA1[int] = lam # Error # v1_1: TA1[int, str] = lam # Error # v1_1: TA1[int, str, None] = lam # Error v1_1(100, 'A')(None) # No error v1_1(None, 100)('A') # Error v1_1('A', None)(100) # Error v1_2: TA1[[None, int], [str]] = lam v1_2(100, 'A')(None) # Error v1_2(None, 100)('A') # No error v1_2('A', None)(100) # Error v1_3: TA1[[str, None], [int]] = lam v1_3(100, 'A')(None) # Error v1_3(None, 100)('A') # Error v1_3('A', None)(100) # No error v1_4: TA1[..., ...] = lam v1_4(100, 'A')(None) # No error v1_4(None, 100)('A') # No error v1_4('A', None)(100) # No error type TA2[**P1=..., **P2=...] = TA1[P1, P2] v2_1: TA2 = lam # v2_1: TA2[..., ...] = lam # v2_1: TA2[] = lam # Error v2_1(100, 'A')(None) # No error v2_1(None, 100)('A') # No error v2_1('A', None)(100) # No error v2_2: TA2[[int, str], [None]] = lam v2_2(100, 'A')(None) # No error v2_2(None, 100)('A') # Error v2_2('A', None)(100) # Error v2_3: TA2[[None, int], [str]] = lam v2_3(100, 'A')(None) # Error v2_3(None, 100)('A') # No error v2_3('A', None)(100) # Error v2_4: TA2[[str, None], [int]] = lam v2_4(100, 'A')(None) # Error v2_4(None, 100)('A') # Error v2_4('A', None)(100) # No error # For the new syntax, Python interpreter and type checkers # don't support `bound`, `covariant` and `contravariant`. type TA[**P:[float, str]] = Callable[P, None] # Error # For the old syntax, type checkers don't support # `bound`, `covariant` and `contravariant`. from typing import ParamSpec P1 = ParamSpec('P1', bound=[float, str]) # Error P2 = ParamSpec('P2', covariant=True) # Error P3 = ParamSpec('P3', contravariant=True) # Error # Run without `--strict` # No type arguments make the type parameters without default types `Any`. from collections.abc import Callable from typing import reveal_type type TA[**P] = Callable[P, None] v: TA = lambda x, y, z: print(x, y, z) reveal_type(v) # def (*Any, **Any)
Top comments (0)