diff options
| author | Ethan Furman <ethan@stoneleaf.us> | 2022-12-06 13:43:41 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-06 13:43:41 -0800 |
| commit | 679efbb080242fc5be63ad873968f05faeef889f (patch) | |
| tree | f3ffc6086b327221e39a732707e47e0eb91da3b8 /Doc | |
| parent | 5da5aa4c3ebcddd1ccbea914f1768a863dc170f0 (diff) | |
| download | cpython-git-679efbb080242fc5be63ad873968f05faeef889f.tar.gz | |
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 'Doc')
| -rw-r--r-- | Doc/howto/enum.rst | 25 | ||||
| -rw-r--r-- | Doc/library/enum.rst | 2 |
2 files changed, 27 insertions, 0 deletions
diff --git a/Doc/howto/enum.rst b/Doc/howto/enum.rst index 98d9f4febe..3155c6cb97 100644 --- a/Doc/howto/enum.rst +++ b/Doc/howto/enum.rst @@ -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 -------- diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index 208aecf11c..f8875440ba 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -389,6 +389,8 @@ Data Types Using :class:`auto` with :class:`Enum` results in integers of increasing value, starting with ``1``. + .. versionchanged:: 3.12 Added :ref:`enum-dataclass-support` + .. class:: IntEnum |
