[issue18929] inspect.classify_class_attrs ignores metaclass

Ethan Furman report at bugs.python.org
Mon Sep 9 01:21:09 CEST 2013


Ethan Furman added the comment:

Antoine, to answer all your questions at once, and using an Enum as the example:

--> dir(Color)
['__class__', '__doc__', '__members__', '__module__', 'blue', 'green', 'red']

--> Color.__members__
mappingproxy(OrderedDict([('red', <Color.red: 1>), ('green', <Color.green: 2>), ('blue', <Color.blue: 3>)]))

-->help(Color)
========================================
Help on class Color in module __main__:

Color = <enum 'Color'>
========================================

Why?  Because __members__ is not in Color.__dict__

--> Color.__dict__.keys()
dict_keys(['_member_type_', '_member_names_', '__new__', '_value2member_map_', '__module__', '_member_map_', '__doc__'])

and inspect.classify_class_attrs doesn't look in metaclasses, instead assigning None as the class if it can't find it:

--> inspect.classify_class_attrs(Color) # output trimmed for brevity
Attribute(name='__members__', kind='data', defining_class=None, object= ... )

So, even though __members__ does show up in dir(Color), classify_class_attrs incorrectly assigns its class.

Can I use classify_class_attrs on Color.__class__? Sure, but then I get 50 items, only one of which was listed in dir(Color).

Interestingly, getmembers(Color) does return __members__, even though it is a metaclass attribute and the docs warn that it won't:

--> print('\n'.join([str(i) for i in inspect.getmembers(Color)]))
('__class__', <attribute '__class__' of 'object' objects>)
('__doc__', None)
('__members__', mappingproxy(OrderedDict([('red', <Color.red: 1>), ('green', <Color.green: 2>), ('blue', <Color.blue: 3>)])))
('__module__', '__main__')
('blue', <Color.blue: 3>)
('green', <Color.green: 2>)
('red', <Color.red: 1>)

So if getmembers() correctly reports it because it shows up in dir(), then classify_class_attrs should correctly identify it as well.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18929>
_______________________________________


More information about the Python-bugs-list mailing list