mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
gh-94943: [Enum] improve repr() when inheriting from a dataclass (GH-99740)
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
This commit is contained in:
parent
5da5aa4c3e
commit
679efbb080
5 changed files with 102 additions and 4 deletions
18
Lib/enum.py
18
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue