[Python-projects] pylint: Crash on module-level @staticmethod/@classmethod
Sylvain Thénault
sylvain.thenault at logilab.fr
Tue Jul 8 10:51:48 CEST 2008
On Tue, May 27, 2008 at 08:08:49PM +0200, Maarten ter Huurne wrote:
>
> Hi,
Hey Maarten!
> 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.
Thank you for you report and for investigating the problem. I've created
a ticket so the fix will be included in the next pylint release:
http://www.logilab.org/ticket/5571
cheers,
--
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