Skip to content

Commit 37ba41b

Browse files
ethanfurmanmiss-islington
authored andcommitted
gh-93910: [Enum] restore member.member restriction while keeping performance boost (GH-94913)
(cherry picked from commit c20186c) Co-authored-by: Ethan Furman <[email protected]>
1 parent e121cb5 commit 37ba41b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Lib/enum.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,14 @@ def __new__(cls, value):
11141114
def __init__(self, *args, **kwds):
11151115
pass
11161116

1117+
def __getattribute__(self, name):
1118+
self_dict = super().__getattribute__('__dict__')
1119+
cls = super().__getattribute__('__class__')
1120+
value = super().__getattribute__(name)
1121+
if isinstance(value, cls) and name not in self_dict and name in self._member_names_:
1122+
raise AttributeError("<enum '%s'> member has no attribute %r" % (cls.__name__, name))
1123+
return super().__getattribute__(name)
1124+
11171125
def _generate_next_value_(name, start, count, last_values):
11181126
"""
11191127
Generate the next value when not given.

Lib/test/test_enum.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2611,14 +2611,19 @@ class Private(Enum):
26112611
self.assertEqual(Private._Private__corporal, 'Radar')
26122612
self.assertEqual(Private._Private__major_, 'Hoolihan')
26132613

2614-
@unittest.skip("Accessing all values retained for performance reasons, see GH-93910")
26152614
def test_exception_for_member_from_member_access(self):
26162615
with self.assertRaisesRegex(AttributeError, "<enum .Di.> member has no attribute .NO."):
26172616
class Di(Enum):
26182617
YES = 1
26192618
NO = 0
26202619
nope = Di.YES.NO
26212620

2621+
def test_no_exception_for_overridden_member_from_member_access(self):
2622+
class Di(Enum):
2623+
YES = 1
2624+
NO = 0
2625+
Di.YES.NO = Di.NO
2626+
nope = Di.YES.NO
26222627

26232628
def test_dynamic_members_with_static_methods(self):
26242629
#

0 commit comments

Comments
 (0)