Pylint false positives

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Aug 14 09:41:41 EDT 2018


On Tue, 14 Aug 2018 10:58:17 +0200, Frank Millman wrote:

>> > 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".
[...]

> I do mean a lot of methods, not classes. I don't have any problem adding
> the lines. It is just that, before I starting using pylint, it had not
> occurred to me that there was any problem with my approach. If an
> experienced python programmer was reviewing my code, would they flag it
> as 'bad style'?

*shrug*

I wouldn't necessarily call it *bad*, but perhaps *not-quite good* style.

I think its fine for a small projects and quick scripts, especially if 
they're written and maintained by a single person for their own use. 
Perhaps not so much for large projects intended for long-term use with 
continual development.

If there really are a lot of such missing methods, I'd consider writing 
something like this:

class A:
    def __init__(self, ...):
        ...

    # === process abstract methods en masse ===
    for name in "method_a method_b method_c method_d".split():
        @abstractmethod
        def inner(self):
            raise NotImplementedError
        inner.__name__ = name
        # This is okay, writing to locals works inside the class body.
        locals()[name] = inner

    del inner, name  # Clean up the class namespace.

    def concrete_method_a(self):
        ...


although to be honest I'm not sure if that would be enough to stop PyLint 
from complaining.


-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list