I'm toying around with implementing monoids with type hinting. To that end I've written:
M = TypeVar('M')
class Monoid(Generic[M]):
...
def append(self, m: 'Monoid[M]') -> 'Monoid[M]':
raise NotImplementedError()
When using this in a subclass, e.g
A = TypeVar('A')
class List(Monoid[A], Generic[A]):
def __init__(self, *values: A) -> None:
self._values = tuple(values)
...
def append(self, m: 'List[A]') -> 'List[A]':
return List(*(self.values + m.values))
I get error: Argument 1 of "append" incompatible with supertype "Monoid"
. Since List
is a proper subclass of Monoid
, I would expect this to be able to type. What am I doing wrong?