ethanfurman / aenum

Advanced Enumerations for Python
183 stars 14 forks source link

MultiValueEnum with attribute as another enum #40

Closed developer992 closed 6 months ago

developer992 commented 6 months ago

why wouldn't this work:

class WarningLevel(enum.Enum):
    INFO = 1
    WARNING = 2
    ERROR = 3

class WarningType2(aenum.MultiValueEnum):
    ONE = 1, "This is rule #1", WarningLevel.INFO
    TWO = 2, "This is rule #2", WarningLevel.ERROR
    THREE = 3, "This is rule #3", WarningLevel.INFO

it fails here but i don't see why this is necessary constraint and further more, how do i get around it?

aenum/_enum.py:973:

if enum_class._multivalue_ and (
        value in enum_class._value2member_map_
        or any(v == value for (v, m) in enum_class._value2member_seq_)
    raise ValueError('%r has already been used' % (value, ))

Not even this works:

class X1(aenum.MultiValueEnum):
    ONE = 1, 1
ethanfurman commented 6 months ago

The purpose of a MultiValueEnum is to have several different values that correspond to one enum member. In your WarningType2 if you tried WarningType2(WarningLevel.INFO it would know if you wanted ONE or THREE.

It looks like what you really want are attributes:

class WarningType2(aenum.Enum):
    _init_ = "value text level"
    ONE = 1, "This is rule #1", WarningLevel.INFO
    TWO = 2, "This is rule #2", WarningLevel.ERROR
    THREE = 3, "This is rule #3", WarningLevel.INFO

and in use:

>>> WarningType2.TWO.level
WarningLevel.ERROR
developer992 commented 6 months ago

oh didn't know that, thanks!