Mahi
Mahi

Reputation: 21941

PyCharm "must implement all abstract methods" on a subclass that's intentionally abstract

I have an abstract base class, Animal:

class Animal(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def move(self):
        raise NotImplementedError()

    @abc.abstractmethod
    def eat(self):
        raise NotImplementedError()

Now I have another abc that only implements one of these methods:

class Bird(Animal):
    def move(self):
        print("fly")

An another class that implements the missing method:

class Eagle(Bird):
    def eat(self):
        print("eagle eats")

But PyCharm is complaining about Bird that it "must implement all abstract methods", when I intentionally want it to stay abstract still.

Am I missing something, or is this a bug? If it's just a bug, can I ignore the warning somehow (similar to #noqa)?

Upvotes: 7

Views: 6862

Answers (1)

gmds
gmds

Reputation: 19905

Just mark Bird as abstract too:

from abc import ABC

class Bird(Animal, ABC):
    def move(self):
        print("fly")

After thinking about it a little, actually, I think that for this purpose it would make more sense to specify metaclass=ABCMeta, as you did originally, since conceptually we do not want to modify the inheritance hierarchy of Bird, but rather mark it as also an abstract class (for PyCharm's benefit), and perhaps that is a cleaner way of doing so.

Upvotes: 5

Related Questions