Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError when raising an exception inside the __init__ method of an enum class #125259

Closed
Viicos opened this issue Oct 10, 2024 · 0 comments
Closed
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@Viicos
Copy link
Contributor

Viicos commented Oct 10, 2024

Bug report

Bug description:

With #111815, any exception raised inside the __init__ method of an enum class 1 is expected to be:

  1. instantiable (not the case with Pydantic ValidationErrors, see Python 3.12 Enum Class Validated Initialization TypeError pydantic/pydantic#10593)
  2. instantiable and expected to take a single argument.

cpython/Lib/enum.py

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

Footnotes

  1. An example is documented as an example here.

@Viicos Viicos added the type-bug An unexpected behavior, bug, or error label Oct 10, 2024
@picnixz picnixz added the stdlib Python modules in the Lib dir label Oct 21, 2024
@ethanfurman ethanfurman added 3.12 bugs and security fixes 3.13 bugs and security fixes needs backport to 3.12 bug and security fixes 3.14 new features, bugs and security fixes needs backport to 3.13 bugs and security fixes labels Oct 22, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 22, 2024
…onGH-125647)

(cherry picked from commit 34653bb)

Co-authored-by: Mario Šaško <mariosasko777@gmail.com>
ethanfurman pushed a commit that referenced this issue Oct 25, 2024
…125647) (GH-125858)

gh-125259: Fix error notes removal in enum initialization (GH-125647)
(cherry picked from commit 34653bb)

Co-authored-by: Mario Šaško <mariosasko777@gmail.com>
ethanfurman pushed a commit to ethanfurman/cpython that referenced this issue Oct 25, 2024
ethanfurman added a commit that referenced this issue Oct 25, 2024
…125647) (GH-125953)

(cherry picked from commit 34653bb)

Co-authored-by: Mario Šaško <mariosasko777@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants