mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
[3.9] bpo-41789: honor object overrides in Enum classes (GH-22250) (GH-22272)
EnumMeta double-checks that `__repr__`, `__str__`, `__format__`, and `__reduce_ex__` are not the same as `object`'s, and replaces them if they are -- even if that replacement was intentionally done in the Enum being constructed. This patch fixes that.
This commit is contained in:
parent
95b81e2f8c
commit
a4677068dd
3 changed files with 14 additions and 1 deletions
|
@ -552,6 +552,14 @@ class TestEnum(unittest.TestCase):
|
|||
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):
|
||||
|
@ -594,7 +602,6 @@ class TestEnum(unittest.TestCase):
|
|||
class Huh(MyStr, MyInt, Enum):
|
||||
One = 1
|
||||
|
||||
|
||||
def test_hash(self):
|
||||
Season = self.Season
|
||||
dates = {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue