Skip to content

Enum: auto() is not activated if combined with other values on assignment line #93464

@huwcbjones

Description

@huwcbjones

Bug report

Creating a Enum that uses automatic values and tuple assignment doesn't work!

from enum import Enum, auto from typing import Any class Day(Enum): def __new__(cls, value, abbr=None): obj = Enum.__new__(cls, value) obj.abbr = abbr return obj @staticmethod def _generate_next_value_(name: str, start: int, count: int, last_values: list[Any]) -> Any: return name MONDAY = auto(), "Mon" # etc print(Day.MONDAY, Day.MONDAY.value, Day.MONDAY.abbr)

Run that snippet and you get

Traceback (most recent call last): File "mwe.py", line 5, in class Day(Enum): File "lib/python3.9/enum.py", line 288, in __new__ enum_member = __new__(enum_class, *args) File "mwe.py", line 8, in __new__ obj = Enum.__new__(cls, value) File "lib/python3.9/enum.py", line 702, in __new__ raise ve_exc ValueError: <enum.auto object at 0x10b673fa0> is not a valid Day 

Your environment

  • CPython versions tested on: 3.9.2/3.9.7/3.10.2
  • Operating system and architecture: Debian 11 (amd64)/macOS 11.4 (amd64)

Metadata

Metadata

Assignees

Labels

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

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions