Search code examples
python-3.xabstract-classmultiple-inheritanceabc

Abstract class, repeated inheritance, specific example


I am working on a codebase that I don't entirely master (yet). I have the following 3-class stucture:

class Processor(ABC):
    @abstractmethod
    def process(self, *args: Any, **kwargs: Any):
        pass


class AbstractEsTask(Processor, ABC):
    def calculate(self, param):
         ...


@DplTask
class EsDirectTask(AbstractEsTask):
    def process(self):
        return self.calculate(param = "DIRECT")

It seems to me that having AbstractEsTask inherit from ABC is superfluous, since Processor already does.

I have tried editing the code accordingly (class AbstractEsTask(Processor, ABC) -> class AbstractEsTask(Processor)) and couldn't observe any change in the output. But, since it's a large codebase, this is probably not enough. Is my understanding (in bold above) correct?


Solution

  • Your statement is correct because when AbstractEsTask inherits Processor it will have access to all values of ABC.

    Or in other words, if python can't find a method in AbstractEsTask, it will first look in Processor, and then ABC. Because Processor has all of the elements that ABC has, ABC will never be needed. If a member is not found in Processor, it will not be found in ABC.

    I cannot think of any edge case where a double inheritance like you describe above would be necessary. Even if Processor modifies one of the methods of ABC, the inheritance will not be affected because Processor is inherited first.

    This is described in the python docs