Skip to content

gh-93910: [Enum] restore member.member restriction while keeping performance boost #94913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,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.
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2646,14 +2646,19 @@ 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):
YES = 1
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):
#
Expand Down