[Interface-dev] ClassProvides vs. inspect.getmembers

Jim Fulton jim at zope.com
Wed Jul 8 13:51:55 EDT 2009

On Jul 8, 2009, at 1:17 PM, Thomas Lotze wrote:

> I just ran into trouble with the following:
>>>> import zope.interface
>>>> import inspect
>>>> class I(zope.interface.Interface):
> ...     pass
> ...
>>>> class Foo(object):
> ...     zope.interface.implements(I)
> ...
>>>> class Bar(Foo):
> ...     pass
> ...
>>>> inspect.getmembers(Bar)
> Traceback (most recent call last):
>  File "<console>", line 1, in <module>
>  File "/opt/Python-2.5/lib/python2.5/inspect.py", line 206, in  
> getmembers
>    value = getattr(object, key)
> AttributeError: __provides__
> This is due to the fact that the ClassProvides declaration  
> deliberately
> raises a AttributeError when queried on a subclass of the class it  
> belongs
> to. I'd like to know people's opinions on whether to change this
> behaviour, perhaps to returning None or something empty in order not  
> to
> break basic inspection.

Inspection is already broken.  It isn't at all invalid for a  
descriptor to raise AttributeError. The inspector should not assume  
that just because something is in the class dict, it is an attribute.

If we were going to work around this inspect bug, the way to do it  
would be to find a way to deal with this without a class __provides__  
attribute, probably by storing the data outside a class. This would be  
a pain, but would be typical of the pain caused by Python's second- 
class treatment of classes.


Jim Fulton
Zope Corporation

More information about the Interface-dev mailing list