diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2013-08-04 15:42:23 (GMT) |
---|---|---|
committer | Ethan Furman <ethan@stoneleaf.us> | 2013-08-04 15:42:23 (GMT) |
commit | 5e5a8230c206e5762b5d4c0708189422c1b1f3b1 (patch) | |
tree | d2e7c6f0cd4a05d60fcd14b4b4daaf6eed897fa8 | |
parent | af760db0670a8891ea160111b24eabcea2088223 (diff) | |
download | cpython-5e5a8230c206e5762b5d4c0708189422c1b1f3b1.zip cpython-5e5a8230c206e5762b5d4c0708189422c1b1f3b1.tar.gz cpython-5e5a8230c206e5762b5d4c0708189422c1b1f3b1.tar.bz2 |
Close #18635: Move class level private attribute from instance to class.
-rw-r--r-- | Lib/enum.py | 2 | ||||
-rw-r--r-- | Lib/test/test_enum.py | 18 |
2 files changed, 19 insertions, 1 deletions
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 |