[Python-checkins] gh-93847: Fix repr of enum of generic aliases (GH-93885)

ethanfurman webhook-mailer at python.org
Thu Jun 16 15:16:22 EDT 2022


https://github.com/python/cpython/commit/138db8e48b0bb006b1561f8ec76ade97afc6cbd7
commit: 138db8e48b0bb006b1561f8ec76ade97afc6cbd7
branch: main
author: Serhiy Storchaka <storchaka at gmail.com>
committer: ethanfurman <ethan at stoneleaf.us>
date: 2022-06-16T12:16:12-07:00
summary:

gh-93847: Fix repr of enum of generic aliases (GH-93885)

files:
A Misc/NEWS.d/next/Library/2022-06-16-09-24-50.gh-issue-93847.kuv8bN.rst
M Lib/enum.py
M Lib/test/test_enum.py

diff --git a/Lib/enum.py b/Lib/enum.py
index ee32d5d4e058b..20fad97e3d199 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -1237,7 +1237,7 @@ def _missing_(cls, value):
         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):
@@ -1512,7 +1512,7 @@ def __len__(self):
 
     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:
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 28594b010d80d..2fadb4eaa1d82 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -7,6 +7,7 @@
 import sys
 import unittest
 import threading
+import typing
 import builtins as bltns
 from collections import OrderedDict
 from datetime import date
@@ -980,6 +981,15 @@ class SpamEnum(Enum):
             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
index 0000000000000..c6947575e67e1
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-06-16-09-24-50.gh-issue-93847.kuv8bN.rst
@@ -0,0 +1 @@
+Fix repr of enum of generic aliases.



More information about the Python-checkins mailing list