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:
Ethan Furman 2022-12-06 13:43:41 -08:00 committed by GitHub
parent 5da5aa4c3e
commit 679efbb080
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 4 deletions

View file

@ -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