-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Enum: auto() is not activated if combined with other values on assignment line #93464
Comments
You can bodge around this by replacing the tuple definition + from enum import Enum, auto
from typing import Any
class Day(Enum):
def _generate_next_value_(name: str, start: int, count: int, last_values: list[Any]) -> Any:
return name
MONDAY = auto()
# etc
@property
def abbr(self):
return {
self.MONDAY: "Mon",
# etc
}[self]
print(Day.MONDAY, Day.MONDAY.value, Day.MONDAY.abbr) prints Day.MONDAY MONDAY Mon as expected |
"auto" is short for automatic, the idea being that you don't need to type anything beyound If you are adding more on the assignment line, such as |
As the current docs say
I didn't care about the exact value in the "complicated" tuple assignment, hence attempting to using auto. If it had been documented that doing |
Fair point. You may also want to check out my 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(abbr="Mon") |
Ta, shall have a look :) |
Just ran into the same issue. There's nothing whatsoever in the docs that would lead you to believe that In my case, it was easier to just manually specify the values (even though I don't care about them), rather than try to work around the issues of An example of such "simple" use of auto that ideally would work: from enum import Enum, auto
class FoodEnum(Enum):
KETCHUP = auto(), "red"
MAYO = auto(), "white"
PICKLES = auto(), "green"
POPCORN = auto(), "white"
def __init__(self, value, color):
self._value_ = value
self.color = color
print(repr(FoodEnum.MAYO)) |
I am convinced that |
…ythonGH-99148) * fix auto() failure during multiple assignment i.e. `ONE = auto(), 'text'` will now have `ONE' with the value of `(1, 'text')`. Before it would have been `(<an auto instance>, 'text')` (cherry picked from commit 8feb7ab) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
) * fix auto() failure during multiple assignment i.e. `ONE = auto(), 'text'` will now have `ONE' with the value of `(1, 'text')`. Before it would have been `(<an auto instance>, 'text')` (cherry picked from commit 8feb7ab) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
(cherry picked from commit e3a3863) Co-authored-by: Ethan Furman <ethan@stoneleaf.us> Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Bug report
Creating a Enum that uses automatic values and tuple assignment doesn't work!
Run that snippet and you get
Your environment
The text was updated successfully, but these errors were encountered: