Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 781
Markus Meskanen Avatar asked May 20 '19 11:05

Markus Meskanen


1 Answers

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.

like image 88
gmds Avatar answered Oct 14 '22 14:10

gmds