I have a generic parent class and a child class that implements the parent with a specific type:
T = TypeVar("T")
class Parent(ABC, Generic[T]):
def get_impl_t(self):
pass
class Child(Parent[int]):
pass
I'd like to get the type of the child class from the parent (see get_impl_t()).
I'm pretty sure this isn't possible without some hackery (inspect.getsource()?) because of type erasure. It's wouldn't work if this didn't have a class hierarchy.
The obvious workaround is add an abstract classmethod that gets the type or add a parameter to the parent class's constructor:
class Parent(ABC, Generic[T]):
def__init__(self, param_cls: Type[T]) -> None:
self.param_cls = param_cls
# or
@classmethod
@abstractmethod
def get_param_cls() -> Type[T]:
pass
This would add some maintenance overhead, so I want to make sure I'm not missing anything.