Skip to content

[Enum] Exception being ignored in custom datatypes #102549

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

Closed
ethanfurman opened this issue Mar 9, 2023 · 2 comments
Closed

[Enum] Exception being ignored in custom datatypes #102549

ethanfurman opened this issue Mar 9, 2023 · 2 comments
Assignees
Labels
3.11 only security fixes 3.12 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ethanfurman
Copy link
Member

ethanfurman commented Mar 9, 2023

import enum

class Base:
    def __init__(self, x):
        print('In Base init')
        raise ValueError("I don't like", x)

class MyEnum(Base, enum.Enum):
    A = 'a'
    def __init__(self, y):
        print('In MyEnum init')
        self.y = y

MyEnum should not be created because of the exception in Base.__init__.

Linked PRs

@ethanfurman ethanfurman added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir 3.11 only security fixes 3.12 only security fixes needs backport to 3.11 only security fixes labels Mar 9, 2023
@ethanfurman ethanfurman self-assigned this Mar 9, 2023
@kwsp
Copy link
Contributor

kwsp commented Mar 9, 2023

Just tested this - MyEnum is indeed not created and the ValueError is raised.

import enum

class Base:
    def __init__(self, x):
        print('In Base init')
        raise ValueError("I don't like", x)

class MyEnum(Base, enum.Enum):
    A = 'a'
    def __init__(self, y):
        print('In MyEnum init')
        self.y = y

MyEnum(1)

Output

In Base init
In MyEnum init
Traceback (most recent call last):
  File "/Users/tnie/code/cpython/tmp.py", line 14, in <module>
    MyEnum(1)
  File "/Users/tnie/code/cpython/Lib/enum.py", line 728, in __call__
    return cls.__new__(cls, value)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tnie/code/cpython/Lib/enum.py", line 1132, in __new__
    raise ve_exc
ValueError: 1 is not a valid MyEnum

What behaviour are you describing that's a bug?

@ethanfurman
Copy link
Member Author

MyEnum should not have been created in the first place. The behavior in the patch I'm working on:

import enum

class Base:
    def __init__(self, x):
        print('In Base init')
        raise ValueError("I don't like", x)

class MyEnum(Base, enum.Enum):
    A = 'a'
    def __init__(self, y):
        print('In MyEnum init')
        self.y = y

and

In Base init
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/source/python/cpython/Lib/enum.py", line 561, in __new__
    raise exc
  File "/source/python/cpython/Lib/enum.py", line 278, in __set_name__
    raise TypeError(
TypeError: _value_ not set in __new__, unable to create it
>>> MyEnum
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'MyEnum' is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 only 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