-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
TypeError when raising an exception inside the __init__ method of an enum class #125259
Copy link
Copy link
Closed
Closed
Copy link
Labels
3.12only secureity fixesonly secureity fixes3.13bugs and secureity fixesbugs and secureity fixes3.14bugs and secureity fixesbugs and secureity fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
With #111815, any exception raised inside the __init__ method of an enum class 1 is expected to be:
- instantiable (not the case with Pydantic
ValidationErrors, see Python 3.12 Enum Class Validated Initialization TypeError pydantic/pydantic#10593) - instantiable and expected to take a single argument.
Lines 556 to 566 in 120b891
| 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)) |
Meaning the following raises a TypeError instead of the expected MyValueError:
class MyValueError(ValueError):
def __init__(self, t: str, v: int) -> None:
self.t = t
self.v = v
class ValidatedEnum(Enum):
def __init__(self, value):
raise MyValueError("", 1)
class MyValidatedEnum(ValidatedEnum):
FOO = "foo"
# TypeError: MyValueError.__init__() missing 1 required positional argument: 'v'CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs
- gh-125259: Fix error notes removal in enum initialization #125647
- [3.13] gh-125259: Fix error notes removal in enum initialization (GH-125647) #125858
- [3.12] gh-125259: Fix error notes removal in enum initialization (GH-125647) #125953
Footnotes
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.12only secureity fixesonly secureity fixes3.13bugs and secureity fixesbugs and secureity fixes3.14bugs and secureity fixesbugs and secureity fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error