0

I've been working on a class that utilises abstract methods. However, I'm getting a mypy error for overwriting the function declaration in each child class. Could someone please explain to me how I can achieve the same effects, whilst also obeying mypy's desires.

from abc import ABC, abstractmethod
from typing import Any

class Base(ABC):
    def __init__(self, *args: Any, **kwargs: Any) -> None:
       # general init stuff for all classes...
       self.myAbstractMethod(*args, **kwargs)

    @abstractmethod
    def myAbstractMethod(self, *args: Any, **kwargs: Any) -> None:
        pass

class Child1(Base):
    def myAbstractMethod(self, x: int, y: int) -> None:
        print(x + y)

class Child2(Base):
    def myAbstractMethod(self, a: str) -> None:
        print(a)

Child1(1, 2)
Child2('hello stackoverflow')

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
lewiswolf
  • 119
  • 1
  • 6
  • 1
    Does this answer your question? [Overriding abstract methods in python](https://stackoverflow.com/questions/50008237/overriding-abstract-methods-in-python) – Mahrkeenerh Oct 06 '21 at 09:14
  • 3
    i think this is simply a case where `Child1` and `Child2` shouldn't be implementations of a common interface. I think you're using the wrong design – joel Oct 06 '21 at 09:24

0 Answers0