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 <[email protected]>
<http://bugs.python.org/issue18929>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com