] > Re: [Python-projects] pylint and interfaces (Logilab.org)

Email Re: [Python-projects] pylint and interfaces

from
to
Enrico Hartung <enrico at iptel dot org>
subject
Re: [Python-projects] pylint and interfaces
date
2005/07/18 10:33
On Monday 11 July à 15:28, Enrico Hartung wrote:
> Hi,

Hi,
 
> I had a problem with interfaces and pylint. The following scenario 
> leaded to an AttributeError:
> (AttributeError: Module instance has no attribute 'get_method')

Hum, I'm unable to reproduce the error using your test case so I guess
it's already fixed in the CVS :) 

> class ITest:
>    "interface"

>    def test1(a, b):
>        pass

>    def test2(c, d):
>        pass


> class Test(ITest):
>     "implementing interface ITest"

>     def __init__:
>         pass

>    def test1(a, b):
>        pass

>    def test2(c, d):
>        pass
>      
> I found out that pylint trys to invoke __init__ in the interface even it 
> does not exist. I think that has something to do with that the default 
> value for method in _ancestors_to_call() (classes.py) is "__init__"...

> I fixed this "bug" by adding a catch for AttributeError in classes.py on 
> line 451:

> def _ancestors_to_call(self, node, klass_node, method='__init__'):
> .
> .
> .
>    else:
>        try:
>            baseastng.get_method(method)
>            to_call[base] = 1
>        except (astng.NotFoundError, AttributeError):
>            continue
> return to_call, unresolved

Hum, getting an attribute error here means there is a bug in pylint...
Anyway, here are some notes about interface handling in pylint :
* a class is considered as an interface by pylint if its name or one of
  its base classes name ends with "Interface" (the same is true for
  exception)
* implemented interfaces for a class are detected using Zope mecanism
  (e.g. __implements__)
* pylint requires self on every methods, both on interface an regular
  classes (this requirement may change for interface classes for Zope
  compatibility...)

So, you're example rewritten to be "pylint aware" looks like the
following :


class Interface: pass

class ITest(Interface):
   "interface"

   def test1(self, a, b):
       pass

   def test2(self, c, d):
       pass


class Test:
   "implementing interface ITest"
   __implements__ = ITest

   def __init__(self):
       pass

   def test1(self, a, b):
       pass

   def test2(self, c, d):
       pass


-- 
Sylvain Thénault                               LOGILAB, Paris (France).

http://www.logilab.com   http://www.logilab.fr  http://www.logilab.org

_______________________________________________
Python-Projects mailing list
Python-Projects@lists.logilab.org
http://lists.logilab.org/mailman/listinfo/python-projects


is a reply to