Is there any way of parametrizing new types created with NewType
, similar to how typing.Sequence
and the like can be parametrized with subscripts? Or of achieving a similar result by different means?
The below example illustrates what I would like to do, but it fails with TypeError: 'function' object is not subscriptable
, because NewType
returns a function.
from typing import NewType, Sequence, TypeVar
T = TypeVar("T")
Type1 = NewType("Type1", float)
Type2 = NewType("Type2", Sequence[T])
Works = Sequence[Type1]
Fails = Type2[Type1]
The reason that I hoped to do this is that I am creating a dataclass where, in __post_init__
, I would like to run some specific logic depending on the type of the attribute.