From c5ed723a778b61e1ec40e557c5b61596d8cba14b Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Thu, 22 Feb 2024 12:58:48 -0800 Subject: [PATCH 1/4] update docs and Enum docs now state to not call super().__new__ if super().__new__ is called, a better error message is now used --- Doc/library/enum.rst | 3 +++ Lib/enum.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index 30d80ce8d488cc..111354d359c331 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -400,6 +400,9 @@ Data Types results in the call ``int('1a', 16)`` and a value of ``17`` for the member. + ..note:: When writing a custom ``__new__``, do use ``super().__new__`` -- + call the appropriate ``__new__`` instead. + .. method:: Enum.__repr__(self) Returns the string used for *repr()* calls. By default, returns the diff --git a/Lib/enum.py b/Lib/enum.py index d10b99615981ba..22963cca4466f2 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -547,7 +547,10 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k classdict['_inverted_'] = None try: exc = None + classdict['_%s__in_progress' % cls] = True enum_class = super().__new__(metacls, cls, bases, classdict, **kwds) + classdict['_%s__in_progress' % cls] = False + delattr(enum_class, '_%s__in_progress' % cls) except Exception as e: # since 3.12 the line "Error calling __set_name__ on '_proto_member' instance ..." # is tacked on to the error instead of raising a RuntimeError @@ -1155,6 +1158,8 @@ def __new__(cls, value): # still not found -- verify that members exist, in-case somebody got here mistakenly # (such as via super when trying to override __new__) if not cls._member_map_: + if getattr(cls, '_%s__in_progress' % cls.__name__, False): + raise TypeError('do not use `super().__new__; call the appropriate __new__ directly') from None raise TypeError("%r has no members defined" % cls) # # still not found -- try _missing_ hook From 79c0e9780515e38c5300d18bbfa2317092e79ffc Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Thu, 22 Feb 2024 14:54:28 -0800 Subject: [PATCH 2/4] add tests --- Lib/test/test_enum.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index cf3e042de1a4b4..27f8bbaf952afc 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -447,7 +447,7 @@ def spam(cls): def test_bad_new_super(self): with self.assertRaisesRegex( TypeError, - 'has no members defined', + 'do not use .super...__new__;', ): class BadSuper(self.enum_type): def __new__(cls, value): @@ -3409,6 +3409,17 @@ def __new__(cls, int_value, *value_aliases): self.assertIs(Types(2), Types.NetList) self.assertIs(Types('nl'), Types.NetList) + def test_no_members(self): + with self.assertRaisesRegex( + TypeError, + 'has no members', + ): + Enum(7) + with self.assertRaisesRegex( + TypeError, + 'has no members', + ): + Flag(7) class TestOrder(unittest.TestCase): "test usage of the `_order_` attribute" From a6ac4af7ec59bf4307327cd3606e8ce8d371de34 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Wed, 28 Feb 2024 12:03:34 -0800 Subject: [PATCH 3/4] Update Doc/library/enum.rst --- Doc/library/enum.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index 111354d359c331..6e7de004cd52a1 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -400,7 +400,7 @@ Data Types results in the call ``int('1a', 16)`` and a value of ``17`` for the member. - ..note:: When writing a custom ``__new__``, do use ``super().__new__`` -- + ..note:: When writing a custom ``__new__``, do not use ``super().__new__`` -- call the appropriate ``__new__`` instead. .. method:: Enum.__repr__(self) From c2005cce11fd3b2a6e6409dd458c7d36807733a0 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Wed, 28 Feb 2024 12:14:37 -0800 Subject: [PATCH 4/4] add news entry --- .../next/Library/2024-02-28-12-14-31.gh-issue-115821.YO2vKA.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-02-28-12-14-31.gh-issue-115821.YO2vKA.rst diff --git a/Misc/NEWS.d/next/Library/2024-02-28-12-14-31.gh-issue-115821.YO2vKA.rst b/Misc/NEWS.d/next/Library/2024-02-28-12-14-31.gh-issue-115821.YO2vKA.rst new file mode 100644 index 00000000000000..7512a09a37cd46 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-02-28-12-14-31.gh-issue-115821.YO2vKA.rst @@ -0,0 +1,2 @@ +[Enum] Improve error message when calling super().__new__() in custom +__new__.