Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pylint W0223: Method ... is abstract in class ... but is not overridden

Tags:

python

pylint

Pylint generates this error for subclasses of an abstract class, even when those subclasses are not themselves instantiated and the methods are overridden in the concrete subclasses. Why does Pylint think my abstract subclasses are intended to be concrete? How can I shut up this warning without getting out the hammer and disabling it altogether in the rc file?

like image 608
AdamC Avatar asked Mar 05 '14 01:03

AdamC


2 Answers

For some reason pylint think the class isn't abstract (currenly detection is done by checking for method which raise NotImplementedError). Adding a comment like #pylint: disable=W0223 at the top of the module (for disabling only in this module) or class (only in this class), should do the trick.

like image 120
sthenault Avatar answered Nov 02 '22 18:11

sthenault


In order to shut up the wrong abstract-method warning you can implement abstract classes with abc.ABC superclass instead of using abc.ABCMeta metaclass.

For example, this code will raise the warning:

from abc import ABCMeta, abstractmethod


class Parent:
    __metaclass__ = ABCMeta

    @abstractmethod
    def do_something(self):
        pass


class Child(Parent):
    __metaclass__ = ABCMeta

but this will not:

from abc import ABC, abstractmethod


class Parent(ABC):

    @abstractmethod
    def do_something(self):
        pass


class Child(Parent, ABC):
    pass

Caution! With ABC superclass and multiple inheritance you have to watch out for potential Method Resolution Order (MRO) problems.

like image 28
Michał Jabłoński Avatar answered Nov 02 '22 19:11

Michał Jabłoński