mirror of
https://github.com/python/cpython.git
synced 2025-11-01 10:45:30 +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
|
|
@ -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
|
||||
--------
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue