[Python-projects] pylint: class methods created by metaclass not recognized
sylvain.thenault at logilab.fr
Mon Oct 29 10:00:03 CET 2007
On Sun, Oct 28, 2007 at 11:26:35AM +0000, Amorilia wrote:
> Pylint 0.13.2 says "Instance of 'XYZ' has no 'xyz' member" when a
> class XYZ has a method xyz created by its metaclass. Attached is a
> very simple example triggering the bug. (The use case I'm interested
> in is a metaclass which parses an xml file in order to serialize,
> among many other things, attribute creation in a class, on which I'm
> getting of course a huge amount of these pylint errors.)
> Aside, I looked into checkers/typecheck.py in an attempt to fix it as
> the error seems to be triggered there. I was a bit lost in the code
> though. Does pylint actually instantiate metaclasses of classes (i.e.
> create the classes) that it must check?
no that's the problem, there is no actual metaclass support in astng
(pylint's code representation). This has been planned for a while now
but is not yet implemented :(
> """Simple metaclass example."""
> from types import MethodType
> class MetaIncrement(type):
> """A metaclass that adds an 'increment' and 'decrement' function to the
> class implementation."""
> def __init__(cls, name, bases, dct):
> """Add an 'increment' function to the class cls."""
> super(MetaIncrement, cls).__init__(name, bases, dict)
> print "instanciating metaclass..."
> print " class : %s" % name
> print " bases : %s" % (bases,)
> print " dict : %s" % dct
> setattr(cls, 'increment', lambda self, k : k+1)
> setattr(cls, 'decrement', lambda self, k : k-1)
> class Test:
> """A basic class with MetaIncrement metaclass."""
> __metaclass__ = MetaIncrement
> def __init__(self):
> """Print functions."""
> print "instanciating class..."
> print " methods:"
> for attrname in dir(self):
> if isinstance(getattr(self, attrname), MethodType):
> print " %s" % attrname
> TESTVAR = Test()
> Python-Projects mailing list
> Python-Projects at lists.logilab.org
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