[Python-projects] pylint: Crash on module-level @staticmethod/@classmethod
Maarten ter Huurne
maarten.ter.huurne at philips.com
Tue May 27 20:08:49 CEST 2008
Hi,
When running pylint on this program:
===
@staticmethod
def f():
pass
===
I get the following exception trace:
===
Traceback (most recent call last):
File "/usr/bin/pylint", line 4, in <module>
lint.Run(sys.argv[1:])
File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 901, in
__init__
linter.check(args)
File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 492, in
check
self.check_astng_module(astng, checkers)
File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 602, in
check_astng_module
if implements(checker, IASTNGChecker)])
File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 619, in
astng_events
self.astng_events(child, checkers, _reversed_checkers)
File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 619, in
astng_events
self.astng_events(child, checkers, _reversed_checkers)
File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 616, in
astng_events
checker.visit(astng)
File "/usr/lib/python2.5/site-packages/logilab/astng/utils.py", line 84,
in visit
method(node)
File "/usr/lib/python2.5/site-packages/pylint/checkers/classes.py", line
206, in visit_function
self._check_first_arg_for_type(node, klass.type == 'metaclass')
AttributeError: Module instance has no attribute 'type'
===
Although it is a bit unusual to have a static method at the module level,
the Python interpreter accepts it.
The same crash happens on @classmethod when used at the model level.
I had a look at checkers/classes.py to see if fixing it would be easy, but
it wasn't clear to me what the proper approach would be:
- let is_method() should return False in cases like this
- don't assume that a method's parent is always a class
As a quick fix, I settled on the latter:
klass_type = getattr(klass, 'type', None) # "klass" might not be a
class
And replace "klass.type" by "klass_type", once in checkers/classes.py and
once in checkers/variables.py.
Versions used:
pylint 0.14.0,
astng 0.17.2, common 0.22.1
Python 2.5.1 (r251:54863, Mar 7 2008, 03:41:45)
Bye,
Maarten
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.logilab.org/pipermail/python-projects/attachments/20080527/63a1df6f/attachment.htm
More information about the Python-Projects
mailing list