From a8d87a75583050ced9abd3d5af5294b920a3d56f Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Wed, 23 Nov 2022 15:15:54 -0800 Subject: [PATCH 1/4] add dataclass support When inheriting from a dataclass, only show the field names in the value section of the member repr(), and not the dataclass' class name. --- Doc/howto/enum.rst | 24 ++++++++++++++++++++++++ Doc/library/enum.rst | 2 ++ Lib/enum.py | 19 ++++++++++++++++++- Lib/test/test_enum.py | 21 ++++++++++++++++++--- 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/Doc/howto/enum.rst b/Doc/howto/enum.rst index 98d9f4febe2dfa..b5cac63e55419d 100644 --- a/Doc/howto/enum.rst +++ b/Doc/howto/enum.rst @@ -459,6 +459,30 @@ sense to allow sharing some common behavior between a group of enumerations. (See `OrderedEnum`_ for an example.) +.. _dataclass_support: + +Dataclass support +----------------- + +When inheriting from a :func:`dataclass` the :func:`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 + + +Use the dataclass option ``repr=False`` to use the standard :func:`repr`. + + Pickling -------- diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index 74d9e67327629e..d4a363286dd93c 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 add :ref:`dataclass_support` + .. class:: IntEnum diff --git a/Lib/enum.py b/Lib/enum.py index 1b683c702d59b4..74c66b5654e8a0 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -955,7 +955,15 @@ def _find_data_repr_(mcls, class_name, bases): return base._value_repr_ elif '__repr__' in base.__dict__: # this is our data repr - return base.__dict__['__repr__'] + # double-check if a dataclass with a default __repr__ + if ( + '__dataclass_fields__' in base.__dict__ + and '__dataclass_params__' in base.__dict__ + and base.__dict__['__dataclass_params__'].repr + ): + return _dataclass_repr + else: + return base.__dict__['__repr__'] return None @classmethod @@ -1551,6 +1559,15 @@ def _power_of_two(value): return False return value == 2 ** _high_bit(value) +def _dataclass_repr(self): + dcf = self.__dataclass_fields__ + return ', '.join( + '%s=%r' % (k, getattr(self, k)) + for k in dcf.keys() + if dcf[k].repr + ) + + def global_enum_repr(self): """ use module.enum_name instead of class.enum_name diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index b6082cf02b18d7..01181e9207cf28 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -2717,17 +2717,32 @@ def upper(self): def test_repr_with_dataclass(self): "ensure dataclass-mixin has correct repr()" - from dataclasses import dataclass - @dataclass + # check overridden dataclass __repr__ is used + from dataclasses import dataclass, field + @dataclass(repr=False) class Foo: __qualname__ = 'Foo' a: int + def __repr__(self): + return 'ha hah!' class Entries(Foo, Enum): ENTRY1 = 1 self.assertTrue(isinstance(Entries.ENTRY1, Foo)) self.assertTrue(Entries._member_type_ is Foo, Entries._member_type_) self.assertTrue(Entries.ENTRY1.value == Foo(1), Entries.ENTRY1.value) - self.assertEqual(repr(Entries.ENTRY1), '') + self.assertEqual(repr(Entries.ENTRY1), '') + # check auto-generated dataclass __repr__ is not used + @dataclass + class CreatureDataMixin: + __qualname__ = 'CreatureDataMixin' + size: str + legs: int + tail: bool = field(repr=False, default=True) + class Creature(CreatureDataMixin, Enum): + __qualname__ = 'Creature' + BEETLE = ('small', 6) + DOG = ('medium', 4) + self.assertEqual(repr(Creature.DOG), "") def test_repr_with_init_data_type_mixin(self): # non-data_type is a mixin that doesn't define __new__ From ee8cfc45391e1f90b2ad92a2c9571f86c986272d Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Wed, 23 Nov 2022 15:46:55 -0800 Subject: [PATCH 2/4] add more tests --- Lib/test/test_enum.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 01181e9207cf28..146ef39ca3846a 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -2717,7 +2717,9 @@ def upper(self): def test_repr_with_dataclass(self): "ensure dataclass-mixin has correct repr()" + # # check overridden dataclass __repr__ is used + # from dataclasses import dataclass, field @dataclass(repr=False) class Foo: @@ -2731,7 +2733,9 @@ class Entries(Foo, Enum): self.assertTrue(Entries._member_type_ is Foo, Entries._member_type_) self.assertTrue(Entries.ENTRY1.value == Foo(1), Entries.ENTRY1.value) self.assertEqual(repr(Entries.ENTRY1), '') + # # check auto-generated dataclass __repr__ is not used + # @dataclass class CreatureDataMixin: __qualname__ = 'CreatureDataMixin' @@ -2743,6 +2747,37 @@ class Creature(CreatureDataMixin, Enum): BEETLE = ('small', 6) DOG = ('medium', 4) self.assertEqual(repr(Creature.DOG), "") + # + # check inherited repr used + # + class Huh: + def __repr__(self): + return 'inherited' + @dataclass(repr=False) + class CreatureDataMixin(Huh): + __qualname__ = 'CreatureDataMixin' + size: str + legs: int + tail: bool = field(repr=False, default=True) + class Creature(CreatureDataMixin, Enum): + __qualname__ = 'Creature' + BEETLE = ('small', 6) + DOG = ('medium', 4) + self.assertEqual(repr(Creature.DOG), "") + # + # check default object.__repr__ used if nothing provided + # + @dataclass(repr=False) + class CreatureDataMixin: + __qualname__ = 'CreatureDataMixin' + size: str + legs: int + tail: bool = field(repr=False, default=True) + class Creature(CreatureDataMixin, Enum): + __qualname__ = 'Creature' + BEETLE = ('small', 6) + DOG = ('medium', 4) + self.assertRegex(repr(Creature.DOG), "") def test_repr_with_init_data_type_mixin(self): # non-data_type is a mixin that doesn't define __new__ From 827013e65f0bb5b998feb5b833a4253a32294737 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 23 Nov 2022 23:58:46 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-11-23-23-58-45.gh-issue-94943.Oog0Zo.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-11-23-23-58-45.gh-issue-94943.Oog0Zo.rst diff --git a/Misc/NEWS.d/next/Library/2022-11-23-23-58-45.gh-issue-94943.Oog0Zo.rst b/Misc/NEWS.d/next/Library/2022-11-23-23-58-45.gh-issue-94943.Oog0Zo.rst new file mode 100644 index 00000000000000..8e0d3a348b5b71 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-11-23-23-58-45.gh-issue-94943.Oog0Zo.rst @@ -0,0 +1 @@ +When inheriting from a dataclass, only show the field names in the value section of the member repr(), and not the dataclass' class name. From a8ead294be6c4e24706940bb6fb9809a2f927213 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Wed, 23 Nov 2022 20:10:12 -0800 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: C.A.M. Gerlach --- Doc/howto/enum.rst | 9 +++++---- Doc/library/enum.rst | 2 +- Lib/enum.py | 1 - .../2022-11-23-23-58-45.gh-issue-94943.Oog0Zo.rst | 6 +++++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Doc/howto/enum.rst b/Doc/howto/enum.rst index b5cac63e55419d..3155c6cb977bf0 100644 --- a/Doc/howto/enum.rst +++ b/Doc/howto/enum.rst @@ -459,13 +459,13 @@ sense to allow sharing some common behavior between a group of enumerations. (See `OrderedEnum`_ for an example.) -.. _dataclass_support: +.. _enum-dataclass-support: Dataclass support ----------------- -When inheriting from a :func:`dataclass` the :func:`repr` omits the inherited -class' name. For example:: +When inheriting from a :class:`~dataclasses.dataclass`, +the :meth:`~Enum.__repr__` omits the inherited class' name. For example:: >>> @dataclass ... class CreatureDataMixin: @@ -480,7 +480,8 @@ class' name. For example:: >>> Creature.DOG -Use the dataclass option ``repr=False`` to use the standard :func:`repr`. +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 d4a363286dd93c..7994b4d2f13c85 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -389,7 +389,7 @@ Data Types Using :class:`auto` with :class:`Enum` results in integers of increasing value, starting with ``1``. - .. versionchanged:: 3.12 add :ref:`dataclass_support` + .. versionchanged:: 3.12 Added :ref:`enum-dataclass-support` .. class:: IntEnum diff --git a/Lib/enum.py b/Lib/enum.py index 74c66b5654e8a0..f07b821f1a606a 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1567,7 +1567,6 @@ def _dataclass_repr(self): if dcf[k].repr ) - def global_enum_repr(self): """ use module.enum_name instead of class.enum_name diff --git a/Misc/NEWS.d/next/Library/2022-11-23-23-58-45.gh-issue-94943.Oog0Zo.rst b/Misc/NEWS.d/next/Library/2022-11-23-23-58-45.gh-issue-94943.Oog0Zo.rst index 8e0d3a348b5b71..ed4754e49bd2cf 100644 --- a/Misc/NEWS.d/next/Library/2022-11-23-23-58-45.gh-issue-94943.Oog0Zo.rst +++ b/Misc/NEWS.d/next/Library/2022-11-23-23-58-45.gh-issue-94943.Oog0Zo.rst @@ -1 +1,5 @@ -When inheriting from a dataclass, only show the field names in the value section of the member repr(), and not the dataclass' class name. +Add :ref:`enum-dataclass-support` to the +:class:`~enum.Enum` :meth:`~enum.Enum.__repr__`. +When inheriting from a :class:`~dataclasses.dataclass`, +only show the field names in the value section of the member :func:`repr`, +and not the dataclass' class name.