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

Weird error when using enum(val1, val2) #107878

Closed
wimglenn opened this issue Aug 11, 2023 · 4 comments
Closed

Weird error when using enum(val1, val2) #107878

wimglenn opened this issue Aug 11, 2023 · 4 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@wimglenn
Copy link
Contributor

wimglenn commented Aug 11, 2023

Using the example from the docs:

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

Actual output (py 3.11):

>>> Color(0, 1)
TypeError: <enum 0> cannot extend <enum 'Color'>

Expected output- not 100% sure but, something along the lines of:

  • TypeError saying "... takes 1 positional argument but 2 were given" like Color() does, or
  • ValueError saying "... is not a valid Color" like Color(0) does

The current metaclass error message is a bit confusing.

@wimglenn wimglenn added the type-bug An unexpected behavior, bug, or error label Aug 11, 2023
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Aug 11, 2023
@JimZhouZZY
Copy link

The error message is correct.
Color(0, 1) calls for creating a new enum subclass with value 0 and names 1. See enum.py line700

    def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1):
        """
        Either returns an existing member, or creates a new enum class.

        This method is used both when an enum class is given a value to match
        to an enumeration member (i.e. Color(3)) and for the functional API
        (i.e. Color = Enum('Color', names='RED GREEN BLUE')).

        When used for the functional API:

        `value` will be the name of the new class.

        `names` should be either a string of white-space/comma delimited names
        (values will start at `start`), or an iterator/mapping of name, value pairs.

        ...
        """
        if names is None:  # simple value lookup
            return cls.__new__(cls, value)
        # otherwise, functional API: we're creating a new Enum type
        return cls._create_(value, names, ...)

According to the doc, subclassing an enumeration is allowed only if the enumeration does not define any members. So in enum.py line930 it raised the error.

@wimglenn
Copy link
Contributor Author

I see that, but it's a leaky abstraction.

@ethanfurman
Copy link
Member

This is fixed in 3.13:

ValueError: (0, 1) is not a valid Color

@wimglenn
Copy link
Contributor Author

wimglenn commented Aug 13, 2023

Right, apologies for not checking main before reporting. Looks fixed in 3.12.0rc1 too, git bisected the fix to 65dab15. Closing as duplicate of [Enum] call syntax error message is confusing #92647.

@wimglenn wimglenn closed this as not planned Won't fix, can't repro, duplicate, stale Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants