ethanfurman / aenum

Advanced Enumerations for Python
183 stars 14 forks source link

Composite Flag entries aren't listed as Enum members #43

Open mwhavens opened 3 months ago

mwhavens commented 3 months ago

When using the Flag enum class, entries that are composite with other entries omits them from the list of all enum entries, even if all entries are unique.

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

[i for i in SBInterfaceTypes]

Result: [<SBInterfaceTypes.UNKNOWN: 1>, <SBInterfaceTypes.URL: 2>, <SBInterfaceTypes.PROD: 4>, <SBInterfaceTypes.DEV: 8>, <SBInterfaceTypes.S3: 16>] T3 is missing

T3 does appear in SBInterfaceType's _member_map_ but not in its _member_names_. Because it's not in the _membernames, the Flag class's __iter__ method will skip it (it loops through all _membernames to use as a key look up for _membermap).

A fix that worked for me was to remove the is_single_bit check in the elif check in the following line of code: https://github.com/ethanfurman/aenum/blob/523f7d4936b16fc598dcde94f55fe3224d0c96b7/aenum/_enum.py#L909

Though I'm not sure what the ramifications outside my use-case are for this change, or why that check was done in the first place.