[Python-checkins] gh-93910: [Enum] restore member.member restriction while keeping performance boost (GH-94913)
miss-islington
webhook-mailer at python.org
Sun Jul 17 00:54:07 EDT 2022
https://github.com/python/cpython/commit/30f28ac296e506b336e0ab56c41422a53c36d0c2
commit: 30f28ac296e506b336e0ab56c41422a53c36d0c2
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022年07月16日T21:53:41-07:00
summary:
gh-93910: [Enum] restore member.member restriction while keeping performance boost (GH-94913)
(cherry picked from commit c20186c3972ff38577c4c5e32ca86748210983d2)
Co-authored-by: Ethan Furman <ethan at stoneleaf.us>
files:
M Lib/enum.py
M Lib/test/test_enum.py
diff --git a/Lib/enum.py b/Lib/enum.py
index e5971917cef46..b19d40cbc5ed9 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -1114,6 +1114,14 @@ def __new__(cls, value):
def __init__(self, *args, **kwds):
pass
+ def __getattribute__(self, name):
+ self_dict = super().__getattribute__('__dict__')
+ cls = super().__getattribute__('__class__')
+ value = super().__getattribute__(name)
+ if isinstance(value, cls) and name not in self_dict and name in self._member_names_:
+ raise AttributeError("<enum '%s'> member has no attribute %r" % (cls.__name__, name))
+ return super().__getattribute__(name)
+
def _generate_next_value_(name, start, count, last_values):
"""
Generate the next value when not given.
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index f9a662746fb1a..74f31bec50c4f 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -2611,7 +2611,6 @@ class Private(Enum):
self.assertEqual(Private._Private__corporal, 'Radar')
self.assertEqual(Private._Private__major_, 'Hoolihan')
- @unittest.skip("Accessing all values retained for performance reasons, see GH-93910")
def test_exception_for_member_from_member_access(self):
with self.assertRaisesRegex(AttributeError, "<enum .Di.> member has no attribute .NO."):
class Di(Enum):
@@ -2619,6 +2618,12 @@ class Di(Enum):
NO = 0
nope = Di.YES.NO
+ def test_no_exception_for_overridden_member_from_member_access(self):
+ class Di(Enum):
+ YES = 1
+ NO = 0
+ Di.YES.NO = Di.NO
+ nope = Di.YES.NO
def test_dynamic_members_with_static_methods(self):
#
More information about the Python-checkins
mailing list