Pylint false positives

Thomas Jollans tjol at tjol.eu
Tue Aug 14 05:05:48 EDT 2018


On 2018-08-14 09:38, Frank Millman wrote:
> Hi all
> 
> Pylint is flagging a lot of lines as errors that I would consider to be
> acceptable.
> 
> I have an abstract class ClassA with a number of concrete sub-classes.
> ClassA has a method which invokes 'self.method_b()' which is defined
> separately on each sub-class. Pylint complains that "Instance of
> 'ClassA' has no  'method_b' member".
> 
> First question - as a matter of style, is Pylint correct? If so, I could
> define 'method_b' in ClassA and raise NotImplementedError. Is this
> considered more pythonic? The downside is that I have quite a few of
> them, so it would add some clutter.

I wouldn't say it's unpythonic per se, but if your ClassA is logically
an abstract base class that requires certain methods to be implemented
in subclasses, then it's probably clearer to use Python's abc [1]
facilities, and declare your abstract methods as actual abstractmethods.

[1]: https://docs.python.org/3/library/abc.html

i.e.

from abc import ABC, abstractmethod
class ClassA(ABC):
    def do_stuff(self):
        return self.method_b(42)**3

    @abstractmethod
    def method_b(self, answer):
        """
        This is a great place to put a docstring
        """

You *can* raise NotImplementedError in your abstractmethods, but I don't
think it's really necessary. You need a statement in the method body of
course, but since you're going to put a docstring there anyway (right?),
that's already taken care of.

-- Thomas


> Second question - if my present code is not unpythonic, is there an easy
> way to suppress the error messages, without disabling 'no-member'
> altogether?



More information about the Python-list mailing list