Skip to content
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
4 changes: 4 additions & 0 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ def __new__(metacls, cls, bases, classdict):

# double check that repr and friends are not the mixin's or various
# things break (such as pickle)
# however, if the method is defined in the Enum itself, don't replace
# it
for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'):
if name in classdict:
continue
class_method = getattr(enum_class, name)
obj_method = getattr(member_type, name, None)
enum_method = getattr(first_enum, name, None)
Expand Down
9 changes: 8 additions & 1 deletion Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,14 @@ def test_format_enum_str(self):
self.assertFormatIsValue('{:>20}', Directional.WEST)
self.assertFormatIsValue('{:<20}', Directional.WEST)

def test_object_str_override(self):
class Colors(Enum):
RED, GREEN, BLUE = 1, 2, 3
def __repr__(self):
return "test.%s" % (self._name_, )
__str__ = object.__str__
self.assertEqual(str(Colors.RED), 'test.RED')

def test_enum_str_override(self):
class MyStrEnum(Enum):
def __str__(self):
Expand Down Expand Up @@ -594,7 +602,6 @@ def repr(self):
class Huh(MyStr, MyInt, Enum):
One = 1


def test_hash(self):
Season = self.Season
dates = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Honor `object` overrides in `Enum` class creation (specifically, `__str__`,
`__repr__`, `__format__`, and `__reduce_ex__`).