From b74af41db76040b680407a07a23e0413fa238f79 Mon Sep 17 00:00:00 2001 From: mariosasko Date: Thu, 17 Oct 2024 16:00:16 +0200 Subject: [PATCH 1/3] Fix error notes removal in enum initialization --- Lib/enum.py | 16 +++++----------- Lib/test/test_enum.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Lib/enum.py b/Lib/enum.py index 17d72738792982..0a1b9fb3f34cca 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -554,22 +554,16 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k classdict['_all_bits_'] = 0 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 - # recreate the exception to discard - exc = type(e)(str(e)) - exc.__cause__ = e.__cause__ - exc.__context__ = e.__context__ - tb = e.__traceback__ - if exc is not None: - raise exc.with_traceback(tb) - # + # since 3.12 the note "Error calling __set_name__ on '_proto_member' instance ..." + # is tacked on to the error instead of raising a RuntimeError, so discard it + if hasattr(e, '__notes__'): + del e.__notes__ + raise # update classdict with any changes made by __init_subclass__ classdict.update(enum_class.__dict__) # diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 5b4a8070526fcf..ba34522921d273 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -1888,6 +1888,26 @@ def test_wrong_inheritance_order(self): class Wrong(Enum, str): NotHere = 'error before this point' + def test_raise_custom_error_on_creation(self): + class InvalidRgbColorError(ValueError): + def __init__(self, r, g, b): + self.r = r + self.g = g + self.b = b + super().__init__(f'({r}, {g}, {b}) is not a valid RGB color') + + with self.assertRaises(InvalidRgbColorError): + class RgbColor(Enum): + RED = (255, 0, 0) + GREEN = (0, 255, 0) + BLUE = (0, 0, 255) + INVALID = (256, 0, 0) + + def __init__(self, r, g, b): + rgb = (r, g, b) + if not all(0 <= val <= 255 for val in rgb): + raise InvalidRgbColorError(*rgb) + def test_intenum_transitivity(self): class number(IntEnum): one = 1 From 054c300bed60d5920b7326b1bd445f5874ab5acf Mon Sep 17 00:00:00 2001 From: mariosasko Date: Thu, 17 Oct 2024 16:39:27 +0200 Subject: [PATCH 2/3] Nit --- Lib/test/test_enum.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index ba34522921d273..19dff3980cbb14 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -1904,9 +1904,8 @@ class RgbColor(Enum): INVALID = (256, 0, 0) def __init__(self, r, g, b): - rgb = (r, g, b) - if not all(0 <= val <= 255 for val in rgb): - raise InvalidRgbColorError(*rgb) + if not all(0 <= val <= 255 for val in (r, g, b)): + raise InvalidRgbColorError(r, g, b) def test_intenum_transitivity(self): class number(IntEnum): From d185ebd8364c7b0422411bfc29b3d88b517e279e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:10:30 +0000 Subject: [PATCH 3/3] =?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/2024-10-17-16-10-29.gh-issue-125259.oMew0c.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-10-17-16-10-29.gh-issue-125259.oMew0c.rst diff --git a/Misc/NEWS.d/next/Library/2024-10-17-16-10-29.gh-issue-125259.oMew0c.rst b/Misc/NEWS.d/next/Library/2024-10-17-16-10-29.gh-issue-125259.oMew0c.rst new file mode 100644 index 00000000000000..4fa6330abea512 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-17-16-10-29.gh-issue-125259.oMew0c.rst @@ -0,0 +1 @@ +Fix the notes removal logic for errors thrown in enum initialization.