Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pylint error with abstract member variable

Tags:

python

pylint

I have the following code:

class A(object):
    def random_function(self):
        print self.name

    def abstract_function(self):
        raise NotImplementedError("This is an abstract class")

class B(A):
    def __init__(self):
        self.name = "Bob"
        super(B, self).__init__()

    def abstract_function(self):
        print "This is not an abstract class"

Pylint reports error:

ID:E1101 A.random_function: Instance of 'A' has no 'name' member

It's true, but I don't care because A is abstract. Is there a way to get rid of this warning without just suppressing it?

Thanks

like image 872
Stefan Avatar asked May 14 '13 07:05

Stefan


3 Answers

It is best to define name in A. Consider somebody (or you in couple weeks) wants to inherit from A and implements abstract_function:

class C(A):
    def abstract_function(self):
        print 'This is not an abstract class'

Now the following will raise an error even though nothing in C seems to be wrong:

c = C()
c.random_function()

If you are using self.name in A it should be defined there (and let's say it should default to something sensible saying it's not ready to use):

class A(object):
    name = None
    def random_function(self):
        print self.name

This will make your code cleaner/less error-prone and you will also get rid of the pylint error.

like image 164
Martin Mrazik Avatar answered Nov 07 '22 15:11

Martin Mrazik


If you suffix A with Mixin, pylint will not report it

like image 32
allan.simon Avatar answered Nov 07 '22 14:11

allan.simon


In your case I'd could use the following option:

pylint solution.py --generated-members=name

However, it's better to consider adding name = None to the base class.

like image 1
Jordan Jambazov Avatar answered Nov 07 '22 13:11

Jordan Jambazov