Discussion:
[Python-projects] pylint W0212 - Access to a protected member of a client class
duncan.gibson at xs4all.nl ()
2006-11-15 20:14:07 UTC
Permalink
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?

Cheers
Duncan

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
Duncan Gibson
2006-11-15 20:14:07 UTC
Permalink
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?

Cheers
Duncan

PS. It's not really too important, but all of the pylint warning messages
that start "To many ..." should really read "Too many..." with two Os.
English is tough stuff
http://alt-usage-english.org/excerpts/fxenglis.html
Duncan Gibson
2006-11-15 20:14:08 UTC
Permalink
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?


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]


Cheers
Duncan

PS. It's not really too important, but all of the pylint warning messages
that start "To many ..." should really read "Too many..." with two Os.
English is tough stuff http://alt-usage-english.org/excerpts/fxenglis.html
Sylvain Thénault
2006-11-16 08:55:18 UTC
Permalink
Post by duncan.gibson at xs4all.nl ()
----------------------------------------------------------------------
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]
----------------------------------------------------------------------
#!/usr/bin/env python
''' example program to ask about pylint message W0212 '''
''' example class '''
_next_id = 0
__slots__ = ('number', 'name')
self.number = Thing.next_id()
self.name = name
return '(number=%d, name="%s")' % (self.number, self.name)
''' static method to increment "private" class attribute '''
Thing._next_id += 1
return Thing._next_id
next_id = staticmethod(next_id)
''' static method to reset "private" class attribute '''
Thing._next_id = 0
reset_id = staticmethod(reset_id)
XX = Thing('one')
print 'XX =', XX
YY = Thing('two')
print 'YY =', YY
Thing.reset_id()
ZZ = Thing('ten')
print 'ZZ =', ZZ
----------------------------------------------------------------------
XX = (number=1, name="one")
YY = (number=2, name="two")
ZZ = (number=1, name="ten")
----------------------------------------------------------------------
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
----------------------------------------------------------------------
: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.
Post by duncan.gibson at xs4all.nl ()
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
Loading...