Close #18635: Move class level private attribute from instance to class.

This commit is contained in:
Ethan Furman 2013-08-04 08:42:23 -07:00
parent af760db067
commit 5e5a8230c2
2 changed files with 19 additions and 1 deletions

View File

@ -127,6 +127,7 @@ class EnumMeta(type):
enum_class = super().__new__(metacls, cls, bases, classdict)
enum_class._member_names_ = [] # names in definition order
enum_class._member_map_ = OrderedDict() # name->value map
enum_class._member_type_ = member_type
# Reverse value->name map for hashable values.
enum_class._value2member_map_ = {}
@ -159,7 +160,6 @@ class EnumMeta(type):
if not hasattr(enum_member, '_value_'):
enum_member._value_ = member_type(*args)
value = enum_member._value_
enum_member._member_type_ = member_type
enum_member._name_ = member_name
enum_member.__init__(*args)
# If another member with the same value was already defined, the

View File

@ -583,6 +583,24 @@ class TestEnum(unittest.TestCase):
option3 = 3
self.assertEqual(int(MailManOptions.option1), 1)
def test_introspection(self):
class Number(IntEnum):
one = 100
two = 200
self.assertIs(Number.one._member_type_, int)
self.assertIs(Number._member_type_, int)
class String(str, Enum):
yarn = 'soft'
rope = 'rough'
wire = 'hard'
self.assertIs(String.yarn._member_type_, str)
self.assertIs(String._member_type_, str)
class Plain(Enum):
vanilla = 'white'
one = 1
self.assertIs(Plain.vanilla._member_type_, object)
self.assertIs(Plain._member_type_, object)
def test_no_such_enum_member(self):
class Color(Enum):
red = 1