Reuben (Matatika)
07/07/2022, 4:22 PMtyping
question:
I have an abstract base class Animal
with a class property siblings
initialised to an empty list
class Animal(metaclass=abc.ABCMeta):
siblings = []
and I want to use typing
to infer the element type of siblings
when sub-classed.
Non-working example, but something like:
T = TypeVar('T', bound='Animal')
class Animal(Generic[T], metaclass=abc.ABCMeta):
siblings: List[T] = []
class Cat(Animal[Cat]): # class X(Y[X]) paradigm seems weird
pass
class Dog(Animal[Dog]):
pass
Cat.siblings # expect type of List[Cat]
Dog.siblings # expect type of List[Dog]