Author ethan.furman
Recipients eli.bendersky, ethan.furman, pitrou
Date 2013-09-08.23:21:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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', < 1>), ('green', < 2>), ('blue', < 3>)]))

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', < 1>), ('green', < 2>), ('blue', < 3>)])))
('__module__', '__main__')
('blue', < 3>)
('green', < 2>)
('red', < 1>)

So if getmembers() correctly reports it because it shows up in dir(), then classify_class_attrs should correctly identify it as well.
Date User Action Args
2013-09-08 23:21:09ethan.furmansetrecipients: + ethan.furman, pitrou, eli.bendersky
2013-09-08 23:21:09ethan.furmansetmessageid: <>
2013-09-08 23:21:09ethan.furmanlinkissue18929 messages
2013-09-08 23:21:08ethan.furmancreate