]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41789: honor object overrides in Enum classes (GH-22250)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 16 Sep 2020 00:16:36 +0000 (17:16 -0700)
committerGitHub <noreply@github.com>
Wed, 16 Sep 2020 00:16:36 +0000 (17:16 -0700)
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.

Automerge-Triggered-By: @ethanfurman
(cherry picked from commit 22415ad62555d79bd583b4a7d6a96006624a8277)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
Lib/enum.py
Lib/test/test_enum.py
Misc/NEWS.d/next/Library/2020-09-14-19-27-46.bpo-41789.pI_uZQ.rst [new file with mode: 0644]

index dfde75048b0f0ad76cc32797d23e485cf6621df0..c892d738f8b3851feb4ca226ec035710bebfeb58 100644 (file)
@@ -249,7 +249,11 @@ class EnumMeta(type):
 
         # 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)
index 13f7ba4949fcc5d319376794351effaba16be14f..32ff32cf1756766ba39f9c9f4b9e07f32aa50457 100644 (file)
@@ -551,6 +551,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):
@@ -593,7 +601,6 @@ class TestEnum(unittest.TestCase):
             class Huh(MyStr, MyInt, Enum):
                 One = 1
 
-
     def test_hash(self):
         Season = self.Season
         dates = {}
diff --git a/Misc/NEWS.d/next/Library/2020-09-14-19-27-46.bpo-41789.pI_uZQ.rst b/Misc/NEWS.d/next/Library/2020-09-14-19-27-46.bpo-41789.pI_uZQ.rst
new file mode 100644 (file)
index 0000000..5ce7a3c
--- /dev/null
@@ -0,0 +1,2 @@
+Honor `object` overrides in `Enum` class creation (specifically, `__str__`,
+`__repr__`, `__format__`, and `__reduce_ex__`).