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

@ -459,6 +459,31 @@ sense to allow sharing some common behavior between a group of enumerations.
(See `OrderedEnum`_ for an example.)
.. _enum-dataclass-support:
Dataclass support
-----------------
When inheriting from a :class:`~dataclasses.dataclass`,
the :meth:`~Enum.__repr__` omits the inherited class' name. For example::
>>> @dataclass
... class CreatureDataMixin:
... size: str
... legs: int
... tail: bool = field(repr=False, default=True)
...
>>> class Creature(CreatureDataMixin, Enum):
... BEETLE = 'small', 6
... DOG = 'medium', 4
...
>>> Creature.DOG
<Creature.DOG: size='medium', legs=4>
Use the :func:`!dataclass` argument ``repr=False``
to use the standard :func:`repr`.
Pickling
--------