From 5e5a8230c206e5762b5d4c0708189422c1b1f3b1 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Sun, 4 Aug 2013 08:42:23 -0700 Subject: Close #18635: Move class level private attribute from instance to class. --- Lib/enum.py | 2 +- Lib/test/test_enum.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Lib/enum.py b/Lib/enum.py index 33af042..34eb7b8 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -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 diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 91c4b69..b959e97 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -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 -- cgit v0.12