I have an abstract baseclass which uses a value whose implementation in different concrete classes can be either an attribute or a property:
from abc import ABC, abstractmethod
class Base(ABC):
n: int
def foo(self):
...
a = ... + self.n
@abstractmethod
def bar(self):
...
class X(Base):
def __init__(self, n: int):
self.n = n
def bar(self):
...
class Y(Base):
@property
def n(self) -> int:
...
def bar(self):
...
The above code (outline, appropriately filled out), works at runtime, but mypy complains about the property in Y
:
error: Signature of "n" incompatible with supertype "Base"
However, I can't remove the n: int
because of foo
, and I also can't put in an abstract property, because this breaks X
. How should I declare the base class to make mypy happy with it?