Skip to content

Enum creation from values fails for certain values. #116040

Closed

Description

@maflAT

Bug report

Bug description:

Hello,
I tried to define an enum with custom initializer and tuple values, akin to the Planet example.
When instantiating this enum from values it works for some values (0, 1) but fails for others (1, 0).

from enum import Enum

class Cardinal(Enum):
    RIGHT = (1, 0)
    UP = (0, 1)
    LEFT = (-1, 0)
    DOWN = (0, -1)

    def __init__(self, x: int, y: int, /) -> None:
        self.x = x
        self.y = y

up = Cardinal(0, 1)  # works
right = Cardinal(1, 0)  # ValueError: 1 is not a valid Cardinal
right_ = Cardinal((1, 0))  # works

I'm not quite sure if I'm allowed to construct this enum from separate x and y parameters or if I need to pass a tuple.
Either way, it should either work for both (up and right) cases or fail for both cases.

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Feb 28, 2024
  2. added
    3.12only security fixes
    3.13only security fixes
    on Feb 28, 2024
  3. hugovk commented on Feb 28, 2024

    @hugovk
    Member

    Reproducible with the latest 3.13 alpha (3.13.0a4).

    (3.11.8 gives TypeError: <enum 0> cannot extend <enum 'Cardinal'> for up = Cardinal(0, 1) # works)

  4. self-assigned this
    on Feb 28, 2024
  5. moved this to In Progress in enum issueson Feb 28, 2024
  6. ethanfurman commented on Feb 28, 2024

    @ethanfurman
    Member

    In 3.11 you still have to use tuples; i.e. Cardinal((0, 1)) -- not having to use a tuple is a 3.12 enhancement.

  7. added 3 commits that reference this issue on Mar 7, 2024
  8. moved this from In Progress to Done in enum issueson Mar 8, 2024
  9. added a commit that references this issue on Mar 11, 2024
  10. added a commit that references this issue on Mar 11, 2024
  11. added a commit that references this issue on Mar 11, 2024
  12. added 2 commits that reference this issue on Mar 25, 2024
  13. added 2 commits that reference this issue on Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions