Skip to content

Commit

Permalink
pythongh-93847: Fix repr of enum of generic aliases (pythonGH-93885)
Browse files Browse the repository at this point in the history
(cherry picked from commit 138db8e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
miss-islington and serhiy-storchaka authored Jun 17, 2022
1 parent 3fbf5c6 commit 0319052
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,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):
Expand Down Expand Up @@ -1515,7 +1515,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:
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
import unittest
import threading
import typing
import builtins as bltns
from collections import OrderedDict
from datetime import date
Expand Down Expand Up @@ -978,6 +979,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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix repr of enum of generic aliases.

0 comments on commit 0319052

Please sign in to comment.