ethanfurman / aenum

Advanced Enumerations for Python
180 stars 13 forks source link

Flag auto() produces wrong value with multiple OR '|' operations. #42

Open mwhavens opened 1 month ago

mwhavens commented 1 month ago

auto() only factors in the last | operation on the right side. I've found a work around is to move the |'s to the left of auto but a fix should probably be done to resolve the code bug as well.

from aenum import Flag, auto
class SBInterfaceTypes(Flag):
    ALL = 0
    UNKNOWN = auto()
    URL = auto()
    PROD = auto()
    DEV = auto()
    S3 = auto()
    T3 = auto() | PROD | S3

list(SBInterfaceTypes._member_map_.values())
print(SBInterfaceTypes.T3)

result: [<SBInterfaceTypes.ALL: 0>, <SBInterfaceTypes.UNKNOWN: 1>, <SBInterfaceTypes.URL: 2>, <SBInterfaceTypes.PROD: 4>, <SBInterfaceTypes.DEV: 8>, <SBInterfaceTypes.S3: 16>, <SBInterfaceTypes.T3: 48>] <SBInterfaceTypes.T3: 48> but should be 52.

32 | 4 | 16 = 52

Using T3 = PROD| S3 | auto() works as expected though.

mwhavens commented 1 month ago

I've tracked the issue to the _resolve(self, base_value=None) function in the _enum.py file. It loops through each step but overwrites the value from the previous iteration. https://github.com/ethanfurman/aenum/blob/523f7d4936b16fc598dcde94f55fe3224d0c96b7/aenum/_enum.py#L725 Adding the following line of code after setting value resolved the issue for me:

  value = op(*values) 
+ base_value = value