From 679efbb080242fc5be63ad873968f05faeef889f Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Tue, 6 Dec 2022 13:43:41 -0800 Subject: gh-94943: [Enum] improve repr() when inheriting from a dataclass (GH-99740) Co-authored-by: C.A.M. Gerlach --- Lib/enum.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'Lib/enum.py') diff --git a/Lib/enum.py b/Lib/enum.py index 1b683c702d5..f07b821f1a6 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 -- cgit v1.2.3