Issue 10006: non-Pythonic fate of abstractmethods (original) (raw)

We ran into this while generating documentation for our project (PyMVPA) with recent sphinx and python2.6 (fine with 2.5, failed for 2.6, 2.7, 3.1), which relies on traversing all attributes given by "dir(obj)", BUT apparently abstractmethods becomes very special -- it is given by "dir(obj)" since it is present in obj.class, but getattr(obj, "abstractmethods") fails for classes derived from type. E.g. following sample demonstrates it:

print("in type's dir" , 'abstractmethods' in dir(type)) print(type.abstractmethods)

class type3(type): pass

print("in type3's dir" , 'abstractmethods' in dir(type3)) print(type3.abstractmethods)

results in output:

$> python2.6 trash/type_subclass.py ("in type's dir", True) <attribute '__abstractmethods__' of 'type' objects> ("in type3's dir", True) Traceback (most recent call last): File "trash/type_subclass.py", line 9, in print(type3.abstractmethods) AttributeError: abstractmethods

$> python3.1 trash/type_subclass.py in type's dir True <attribute '__abstractmethods__' of 'type' objects> in type3's dir True Traceback (most recent call last): File "trash/type_subclass.py", line 9, in print(type3.abstractmethods) AttributeError: abstractmethods

And that seems to be the only attribute behaving like that (others are fine and accessible). Some people even seems to provide workarounds already, e.g.: http://bitbucket.org/DasIch/bpython-colorful/src/19bb4cb0a65d/bpython/repl.py when abstractmethods is accessed only for the subclasses of ABCmeta ...

so, is it a bug or a feature (so we have to take care about it in all traversals of attributes given by dir())? ;)