How do I annotate the type of a function parameter of a abstractmethod, when the parameter can have any type derived from a specific base type?
Example:
import abc
import attr
@attr.s(auto_attribs=True)
class BaseConfig(abc.ABC):
option_base: str
@attr.s(auto_attribs=True)
class ConfigA(BaseConfig):
option_a: str
@attr.s(auto_attribs=True)
class ConfigB(BaseConfig):
option_b: bool
class Base(abc.ABC):
@abc.abstractmethod
def do_something(self, config: BaseConfig):
pass
class ClassA(Base):
def do_something(self, config: ConfigA):
# test.py:27: error: Argument 1 of "do_something" is incompatible with supertype "Base"; supertype defines the argument type as "BaseConfig"
print("option_a: " + config.option_a)
class ClassB(Base):
def do_something(self, config: ConfigB):
# test.py:33: error: Argument 1 of "do_something" is incompatible with supertype "Base"; supertype defines the argument type as "BaseConfig"
print("option_b: " + str(config.option_b))
conf_a = ConfigA(option_a="value_a", option_base="value_base")
conf_b = ConfigB(option_b=True, option_base="value_base")
object_a = ClassA()
object_b = ClassB()
object_a.do_something(conf_a)
object_b.do_something(conf_b)
When parsing this with mypy I get
test.py:27: error: Argument 1 of "do_something" is incompatible with supertype "Base"; supertype defines the argument type as "BaseConfig"
test.py:33: error: Argument 1 of "do_something" is incompatible with supertype "Base"; supertype defines the argument type as "BaseConfig"
How would I need to change the signature of Base.do_something() so mypy doesn't report any error, while still enforcing, that the function parameter of the abstract method do_something is derived from BaseConfig?