From 8f7db993dee3661c1fb066903d9cdebcb72512ae Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 2 Feb 2024 08:57:19 +0300 Subject: [PATCH 1/4] gh-114803: Mention that `@dataclass` should not be applied on enums --- Doc/howto/enum.rst | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Doc/howto/enum.rst b/Doc/howto/enum.rst index 1e9ac9b6761b64..48357b7355518b 100644 --- a/Doc/howto/enum.rst +++ b/Doc/howto/enum.rst @@ -497,13 +497,23 @@ 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 :type:`Enum` + and its subclasses is not supported. It will not raise any errors, + but it will produce very strange results at runtime:: + + @dataclass # don't do this: it does not make any sense + class Colors(Enum): + red = 'red' + Pickling -------- From d0d13a72a42f30c1626bfe2e29781e408f2ff97f Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Fri, 2 Feb 2024 09:46:18 +0300 Subject: [PATCH 2/4] Update Doc/howto/enum.rst Co-authored-by: Kirill Podoprigora --- Doc/howto/enum.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/howto/enum.rst b/Doc/howto/enum.rst index 48357b7355518b..4205e60c3de15b 100644 --- a/Doc/howto/enum.rst +++ b/Doc/howto/enum.rst @@ -506,7 +506,7 @@ to use the standard :func:`repr`. .. note:: - Adding :func:`~dataclasses.dataclass` decorator to :type:`Enum` + 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:: From 4c0aa90dcf90b994461bfce1a32ddbaf3d2db660 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sat, 3 Feb 2024 02:05:02 +0300 Subject: [PATCH 3/4] Update Doc/howto/enum.rst Co-authored-by: Ethan Furman --- Doc/howto/enum.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/howto/enum.rst b/Doc/howto/enum.rst index 4205e60c3de15b..8898b9045fda71 100644 --- a/Doc/howto/enum.rst +++ b/Doc/howto/enum.rst @@ -508,7 +508,8 @@ to use the standard :func:`repr`. 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:: + 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 Colors(Enum): From d13dd7f350a6236b091fb3814ba7bb77959949de Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 3 Feb 2024 13:55:45 +0300 Subject: [PATCH 4/4] Address review --- Doc/howto/enum.rst | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Doc/howto/enum.rst b/Doc/howto/enum.rst index 48357b7355518b..65efffd3149d0e 100644 --- a/Doc/howto/enum.rst +++ b/Doc/howto/enum.rst @@ -510,9 +510,15 @@ to use the standard :func:`repr`. and its subclasses is not supported. It will not raise any errors, but it will produce very strange results at runtime:: - @dataclass # don't do this: it does not make any sense - class Colors(Enum): - red = 'red' + >>> @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