Skip to content

[3.7] bpo-34282: Fix Enum._convert shadowing members named _convert #9034

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 6 commits into from
Sep 10, 2018
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
10 changes: 6 additions & 4 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ def __new__(metacls, cls, bases, classdict):
enum_class._member_map_ = OrderedDict() # name->value map
enum_class._member_type_ = member_type

# save attributes from super classes so we know if we can take
# the shortcut of storing members in the class dict
base_attributes = {a for b in enum_class.mro() for a in b.__dict__}
# save DynamicClassAttribute attributes from super classes so we know
# if we can take the shortcut of storing members in the class dict
dynamic_attributes = {k for c in enum_class.mro()
for k, v in c.__dict__.items()
if isinstance(v, DynamicClassAttribute)}

# Reverse value->name map for hashable values.
enum_class._value2member_map_ = {}
Expand Down Expand Up @@ -233,7 +235,7 @@ def __new__(metacls, cls, bases, classdict):
enum_class._member_names_.append(member_name)
# performance boost for any member that would not shadow
# a DynamicClassAttribute
if member_name not in base_attributes:
if member_name not in dynamic_attributes:
setattr(enum_class, member_name, enum_member)
# now add to _member_map_
enum_class._member_map_[member_name] = enum_member
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,23 @@ class MoreColor(Color):
yellow = 6
self.assertEqual(MoreColor.magenta.hex(), '5 hexlified!')

def test_subclass_duplicate_name(self):
class Base(Enum):
def test(self):
pass
class Test(Base):
test = 1
self.assertIs(type(Test.test), Test)

def test_subclass_duplicate_name_dynamic(self):
from types import DynamicClassAttribute
class Base(Enum):
@DynamicClassAttribute
def test(self):
return 'dynamic'
class Test(Base):
test = 1
self.assertEqual(Test.test.test, 'dynamic')

def test_no_duplicates(self):
class UniqueEnum(Enum):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix enum members getting shadowed by parent attributes.