]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-93847: Fix repr of enum of generic aliases (GH-93885)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 17 Jun 2022 05:00:22 +0000 (22:00 -0700)
committerGitHub <noreply@github.com>
Fri, 17 Jun 2022 05:00:22 +0000 (22:00 -0700)
(cherry picked from commit 138db8e48b0bb006b1561f8ec76ade97afc6cbd7)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/enum.py
Lib/test/test_enum.py
Misc/NEWS.d/next/Library/2022-06-16-09-24-50.gh-issue-93847.kuv8bN.rst [new file with mode: 0644]

index 091acb3e7f343208d90162b964fac357ed252ab6..1dc3a0e5dc4d8ee4dbfe67167a1e0408be86bd30 100644 (file)
@@ -1240,7 +1240,7 @@ class Enum(metaclass=EnumType):
         return None
 
     def __repr__(self):
-        v_repr = self.__class__._value_repr_ or self._value_.__class__.__repr__
+        v_repr = self.__class__._value_repr_ or repr
         return "<%s.%s: %s>" % (self.__class__.__name__, self._name_, v_repr(self._value_))
 
     def __str__(self):
@@ -1515,7 +1515,7 @@ class Flag(Enum, boundary=STRICT):
 
     def __repr__(self):
         cls_name = self.__class__.__name__
-        v_repr = self.__class__._value_repr_ or self._value_.__class__.__repr__
+        v_repr = self.__class__._value_repr_ or repr
         if self._name_ is None:
             return "<%s: %s>" % (cls_name, v_repr(self._value_))
         else:
index b44c5664e5104eccc28b53bbf7e644102be9ee88..66c77675cf352feaa502283cc2cacf98379d11e8 100644 (file)
@@ -7,6 +7,7 @@ import pydoc
 import sys
 import unittest
 import threading
+import typing
 import builtins as bltns
 from collections import OrderedDict
 from datetime import date
@@ -978,6 +979,15 @@ class TestSpecial(unittest.TestCase):
             spam = SpamEnumNotInner
         self.assertEqual(SpamEnum.spam.value, SpamEnumNotInner)
 
+    def test_enum_of_generic_aliases(self):
+        class E(Enum):
+            a = typing.List[int]
+            b = list[int]
+        self.assertEqual(E.a.value, typing.List[int])
+        self.assertEqual(E.b.value, list[int])
+        self.assertEqual(repr(E.a), '<E.a: typing.List[int]>')
+        self.assertEqual(repr(E.b), '<E.b: list[int]>')
+
     @unittest.skipIf(
             python_version >= (3, 13),
             'inner classes are not members',
diff --git a/Misc/NEWS.d/next/Library/2022-06-16-09-24-50.gh-issue-93847.kuv8bN.rst b/Misc/NEWS.d/next/Library/2022-06-16-09-24-50.gh-issue-93847.kuv8bN.rst
new file mode 100644 (file)
index 0000000..c694757
--- /dev/null
@@ -0,0 +1 @@
+Fix repr of enum of generic aliases.