summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2022-12-06 21:43:41 (GMT)
committerGitHub <noreply@github.com>2022-12-06 21:43:41 (GMT)
commit679efbb080242fc5be63ad873968f05faeef889f (patch)
treef3ffc6086b327221e39a732707e47e0eb91da3b8 /Lib
parent5da5aa4c3ebcddd1ccbea914f1768a863dc170f0 (diff)
downloadcpython-679efbb080242fc5be63ad873968f05faeef889f.zip
cpython-679efbb080242fc5be63ad873968f05faeef889f.tar.gz
cpython-679efbb080242fc5be63ad873968f05faeef889f.tar.bz2
gh-94943: [Enum] improve repr() when inheriting from a dataclass (GH-99740)
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/enum.py18
-rw-r--r--Lib/test/test_enum.py56
2 files changed, 70 insertions, 4 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index 1b683c7..f07b821 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -955,7 +955,15 @@ class EnumType(type):
return base._value_repr_
elif '__repr__' in base.__dict__:
# this is our data repr
- return base.__dict__['__repr__']
+ # double-check if a dataclass with a default __repr__
+ if (
+ '__dataclass_fields__' in base.__dict__
+ and '__dataclass_params__' in base.__dict__
+ and base.__dict__['__dataclass_params__'].repr
+ ):
+ return _dataclass_repr
+ else:
+ return base.__dict__['__repr__']
return None
@classmethod
@@ -1551,6 +1559,14 @@ def _power_of_two(value):
return False
return value == 2 ** _high_bit(value)
+def _dataclass_repr(self):
+ dcf = self.__dataclass_fields__
+ return ', '.join(
+ '%s=%r' % (k, getattr(self, k))
+ for k in dcf.keys()
+ if dcf[k].repr
+ )
+
def global_enum_repr(self):
"""
use module.enum_name instead of class.enum_name
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index b6082cf..146ef39 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -2717,17 +2717,67 @@ class TestSpecial(unittest.TestCase):
def test_repr_with_dataclass(self):
"ensure dataclass-mixin has correct repr()"
- from dataclasses import dataclass
- @dataclass
+ #
+ # check overridden dataclass __repr__ is used
+ #
+ from dataclasses import dataclass, field
+ @dataclass(repr=False)
class Foo:
__qualname__ = 'Foo'
a: int
+ def __repr__(self):
+ return 'ha hah!'
class Entries(Foo, Enum):
ENTRY1 = 1
self.assertTrue(isinstance(Entries.ENTRY1, Foo))
self.assertTrue(Entries._member_type_ is Foo, Entries._member_type_)
self.assertTrue(Entries.ENTRY1.value == Foo(1), Entries.ENTRY1.value)
- self.assertEqual(repr(Entries.ENTRY1), '<Entries.ENTRY1: Foo(a=1)>')
+ self.assertEqual(repr(Entries.ENTRY1), '<Entries.ENTRY1: ha hah!>')
+ #
+ # check auto-generated dataclass __repr__ is not used
+ #
+ @dataclass
+ class CreatureDataMixin:
+ __qualname__ = 'CreatureDataMixin'
+ size: str
+ legs: int
+ tail: bool = field(repr=False, default=True)
+ class Creature(CreatureDataMixin, Enum):
+ __qualname__ = 'Creature'
+ BEETLE = ('small', 6)
+ DOG = ('medium', 4)
+ self.assertEqual(repr(Creature.DOG), "<Creature.DOG: size='medium', legs=4>")
+ #
+ # check inherited repr used
+ #
+ class Huh:
+ def __repr__(self):
+ return 'inherited'
+ @dataclass(repr=False)
+ class CreatureDataMixin(Huh):
+ __qualname__ = 'CreatureDataMixin'
+ size: str
+ legs: int
+ tail: bool = field(repr=False, default=True)
+ class Creature(CreatureDataMixin, Enum):
+ __qualname__ = 'Creature'
+ BEETLE = ('small', 6)
+ DOG = ('medium', 4)
+ self.assertEqual(repr(Creature.DOG), "<Creature.DOG: inherited>")
+ #
+ # check default object.__repr__ used if nothing provided
+ #
+ @dataclass(repr=False)
+ class CreatureDataMixin:
+ __qualname__ = 'CreatureDataMixin'
+ size: str
+ legs: int
+ tail: bool = field(repr=False, default=True)
+ class Creature(CreatureDataMixin, Enum):
+ __qualname__ = 'Creature'
+ BEETLE = ('small', 6)
+ DOG = ('medium', 4)
+ self.assertRegex(repr(Creature.DOG), "<Creature.DOG: .*CreatureDataMixin object at .*>")
def test_repr_with_init_data_type_mixin(self):
# non-data_type is a mixin that doesn't define __new__