https://linen.dev logo
#random
Title
# random
r

Reuben (Matatika)

07/07/2022, 4:22 PM
Quick Python
typing
question: I have an abstract base class
Animal
with a class property
siblings
initialised to an empty
list
Copy code
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:
Copy code
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]