I'm using this pattern often:
import typing
T = typing.TypeVar('T')
class Base(typing.Generic[T]):
_type: typing.Type[T]
def func(self) -> T:
return self._type(42)
the_type: type = int
class A(Base[the_type]):
_type = the_type
the_type = str
class B(Base[the_type]):
_type = the_type
Is there any way to avoid passing the type to both Base[]
and as _type
?
I want something like this:
import typing
T = typing.TypeVar('T')
class Base(typing.Generic[T]):
def func(self) -> T:
return T(42)
class A(Base[int]):
pass
class B(Base[str]):
pass
but that's not how it works of course.