From: Nikita Sobolev Date: Sat, 3 Feb 2024 21:55:38 +0000 (+0300) Subject: gh-114803: Mention that `@dataclass` should not be applied on enums (GH-114891) X-Git-Tag: v3.13.0a4~173 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=72d2d0f10d5623bceb98a2014926ea0b87594ecb;p=thirdparty%2FPython%2Fcpython.git gh-114803: Mention that `@dataclass` should not be applied on enums (GH-114891) Co-authored-by: Kirill Podoprigora Co-authored-by: Ethan Furman --- diff --git a/Doc/howto/enum.rst b/Doc/howto/enum.rst index 1e9ac9b6761b..30be15230fc0 100644 --- a/Doc/howto/enum.rst +++ b/Doc/howto/enum.rst @@ -497,13 +497,30 @@ the :meth:`~Enum.__repr__` omits the inherited class' name. For example:: >>> Creature.DOG -Use the :func:`!dataclass` argument ``repr=False`` +Use the :func:`~dataclasses.dataclass` argument ``repr=False`` to use the standard :func:`repr`. .. versionchanged:: 3.12 Only the dataclass fields are shown in the value area, not the dataclass' name. +.. note:: + + Adding :func:`~dataclasses.dataclass` decorator to :class:`Enum` + and its subclasses is not supported. It will not raise any errors, + but it will produce very strange results at runtime, such as members + being equal to each other:: + + >>> @dataclass # don't do this: it does not make any sense + ... class Color(Enum): + ... RED = 1 + ... BLUE = 2 + ... + >>> Color.RED is Color.BLUE + False + >>> Color.RED == Color.BLUE # problem is here: they should not be equal + True + Pickling --------