aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/enum.py
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2022-12-06 13:43:41 -0800
committerGitHub <noreply@github.com>2022-12-06 13:43:41 -0800
commit679efbb080242fc5be63ad873968f05faeef889f (patch)
treef3ffc6086b327221e39a732707e47e0eb91da3b8 /Lib/enum.py
parent5da5aa4c3ebcddd1ccbea914f1768a863dc170f0 (diff)
downloadcpython-679efbb080242fc5be63ad873968f05faeef889f.tar.gz
cpython-679efbb080242fc5be63ad873968f05faeef889f.zip
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/enum.py')
-rw-r--r--Lib/enum.py18
1 files changed, 17 insertions, 1 deletions
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