[issue19030] Make inspect.getmembers and inspect.classify_class_attrs Enum aware

Ethan Furman report at bugs.python.org
Mon Sep 16 02:00:54 CEST 2013


New submission from Ethan Furman:

Due to the odd nature of Enum classes and instances, the normal methods used by inspect.getmembers and inspect.classify_class_attrs are insufficient.

By special casing Enum inside those two functions the correct information can be returned.

Here is an example of the problem:

=======================================================================================
--> class Test(enum.Enum):
...   this = 'that'
...   these = 'those'
...   whose = 'mine'
...   name = 'Python'
...   value = 'awesome'
...   def what(self):
...     return "%s is %s!" % (self.name, self.value)
... 

--> help(Test)
Help on Test in module __main__ object:

class Test(enum.Enum)
 |  Method resolution order:
 |      Test
 |      enum.Enum
 |      builtins.object
 |  
 |  Data and other attributes defined here:
 |  
 |  these = <Test.these: 'those'>
 |  
 |  this = <Test.this: 'that'>
 |  
 |  whose = <Test.whose: 'mine'>
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from enum.Enum:
 |  
 |  name
 |      The name of the Enum member.
 |  
 |  value
 |      The value of the Enum member.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from enum.EnumMeta:
 |  
 |  __members__
 |      Returns a mapping of member name->value.
 |      
 |      This mapping lists all enum members, including aliases. Note that this
 |      is a read-only view of the internal mapping.
(END)
=======================================================================================

As can be seen, 'name' and 'value' are not showing up as enum members.

----------
messages: 197844
nosy: barry, eli.bendersky, ethan.furman, ncoghlan, pitrou
priority: normal
severity: normal
status: open
title: Make inspect.getmembers and inspect.classify_class_attrs Enum aware
type: behavior
versions: Python 3.4

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


More information about the Python-bugs-list mailing list