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)?
-
It's not a bug. The entire concept of abstract classes is to force subclasses to implement all methods.DeepSpace– DeepSpace2019年05月20日 11:15:23 +00:00Commented May 20, 2019 at 11:15
-
@DeepSpace How about my example then? I should be allowed to have a chain of multiple abc's.Mahi– Mahi2019年05月20日 11:25:05 +00:00Commented May 20, 2019 at 11:25
1 Answer 1
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.
5 Comments
ABC only seems to add metaclass=abc.ABCMeta, and metaclasses inherit to all subclasses, so this doesn't do anything in Python. It did cheat PyCharm though, so thank you! :)ABC; yup, that's all it does, but I personally find it more convenient than specifying the metaclass explicitly. About the actual effect, you're right that it doesn't actually do anything. However, PyCharm, by default, assumes that all classes that inherit from abstract classes should be concrete, so this tells PyCharm that this class is specifically still meant to be abstract. As for the inheritance hierarchy, since, as you noted, all this does is change the metaclass, so perhaps using the metaclass method is better, from a principled perspective ;)metaclass over subclassing ABC, but unfortunately it didn't cheat PyCharm, it was the first thing I tried before posting the question. I'm not sure why subclassing ABC works but re-defining the metaclass doesn't, but I'm glad you came up with it.Explore related questions
See similar questions with these tags.