[Python-projects] pylint W0212 - Access to a protected member of a client class
Sylvain Thénault
sylvain.thenault at logilab.fr
Thu Nov 16 09:55:18 CET 2006
On Wednesday 08 November à 19:01, duncan.gibson at xs4all.nl wrote:
>
> pylint --version gives:
> ----------------------------------------------------------------------
>
> pylint 0.12.1, astng 0.16.1, common 0.19.2
> Python 2.5 (r25:51908, Oct 11 2006, 16:04:20)
> [GCC 3.4.6]
>
>
> If I run the following example Python script:
> ----------------------------------------------------------------------
>
> #!/usr/bin/env python
> ''' example program to ask about pylint message W0212 '''
>
> class Thing(object):
> ''' example class '''
>
> _next_id = 0
>
> __slots__ = ('number', 'name')
>
> def __init__(self, name):
> self.number = Thing.next_id()
> self.name = name
>
> def __str__(self):
> return '(number=%d, name="%s")' % (self.number, self.name)
>
> def next_id():
> ''' static method to increment "private" class attribute '''
> Thing._next_id += 1
> return Thing._next_id
> next_id = staticmethod(next_id)
>
> def reset_id():
> ''' static method to reset "private" class attribute '''
> Thing._next_id = 0
> reset_id = staticmethod(reset_id)
>
>
> if __name__ == '__main__':
>
> XX = Thing('one')
> print 'XX =', XX
> YY = Thing('two')
> print 'YY =', YY
>
> Thing.reset_id()
> ZZ = Thing('ten')
> print 'ZZ =', ZZ
>
>
> then I get the following output, as expected:
> ----------------------------------------------------------------------
>
> XX = (number=1, name="one")
> YY = (number=2, name="two")
> ZZ = (number=1, name="ten")
>
>
> If I then run pylint on the script, I get two W0212 messages:
> ----------------------------------------------------------------------
>
> W: 20:Thing.next_id: Access to a protected member _next_id of a client class
> W: 21:Thing.next_id: Access to a protected member _next_id of a client class
>
>
> So I ran pylint --help-message=W0212 to check the warning:
> ----------------------------------------------------------------------
>
> :W0212: *Access to a protected member %s of a client class*
> Used when a protected member (i.e. class member with a name beginning with an
> underscore) is access outside the class or a descendant of the class where
> it's defined. This message belongs to the classes checker.
>
>
> In my original code, I was accessing the "private" class attribute
> directly from outside the class, received W0212 messages, and to avoid
> the warnings I encapsulated the access in "public" static methods like
> those above, and *still* got the warning messages.
>
> And now that I've typed in all of this text, I realise that pylint gives
> the warnings for accessing the private _next_id class attribute in the
> next_id() static method, but not for accessing it in the reset_id() method!
>
> What is the expected behaviour? Should both give warnings, or neither?
The warning is not printed into reset_id since there's only an assigment
there, no access. I'm not sure we would like a warning even if we are
actually outside the class. Opinion someone ? Secondly I would have
expected 'as you did that your encpsulation fix the warning. This is a
bug, I've added it to the tracker.
> PS. It's not too important, but all of the "To many ..." warning messages
> should really read "Too many..." with two Os.
> English is tough stuff http://alt-usage-english.org/excerpts/fxenglis.html
those are already fixed in the repository.
--
Sylvain Thénault LOGILAB, Paris (France)
Formations Python, Zope, Plone, Debian: http://www.logilab.fr/formations
Développement logiciel sur mesure: http://www.logilab.fr/services
Python et calcul scientifique: http://www.logilab.fr/science
More information about the Python-Projects
mailing list