summaryrefslogtreecommitdiffstats
path: root/Doc/howto
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2022-12-06 21:43:41 (GMT)
committerGitHub <noreply@github.com>2022-12-06 21:43:41 (GMT)
commit679efbb080242fc5be63ad873968f05faeef889f (patch)
treef3ffc6086b327221e39a732707e47e0eb91da3b8 /Doc/howto
parent5da5aa4c3ebcddd1ccbea914f1768a863dc170f0 (diff)
downloadcpython-679efbb080242fc5be63ad873968f05faeef889f.zip
cpython-679efbb080242fc5be63ad873968f05faeef889f.tar.gz
cpython-679efbb080242fc5be63ad873968f05faeef889f.tar.bz2
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/howto')
-rw-r--r--Doc/howto/enum.rst25
1 files changed, 25 insertions, 0 deletions
diff --git a/Doc/howto/enum.rst b/Doc/howto/enum.rst
index 98d9f4f..3155c6c 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
--------