On 8/14/2018 5:05 AM, Thomas Jollans wrote:
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.

If I were reading you class, I would like to see all methods defined there. If you went the old NotImplemented(Error) route, you could avoid cluttter with

    method_c = method_d = method_e = method_b

but I would also want docstrings. At that point, I would consider abstractmethod. But I have not used that, not seen an stdlib class that does.

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?


--
Terry Jan Reedy


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to