Skip to content

Commit

Permalink
Fixed @enum_bitmask crash on Python 3.10 for __annotations__ member.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamshapiro0 committed Mar 25, 2024
1 parent 1ed02b3 commit 5664946
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions python/fusion_engine_client/utils/enum_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,26 +188,30 @@ def to_string(cls, mask: int) -> str:
WrappedCls._enum_offset = offset
WrappedCls._enum_values = [v for v in enum_type if predicate(v)]

# List elements returned by getmembers() for _all_ objects so we can skip them in the loops below when looking
# List elements returned by getmembers() for _all_ IntEnum so we can skip them in the loops below when looking
# for enum values to be added.
internals = set(dir(type('dummy', (object,), {})))
internals.add('__members__')
class Dummy(IntEnum):
pass
internals = [e[0] for e in inspect.getmembers(Dummy)]

def _is_enum_entry(entry):
return entry[0] not in internals and not entry[0].startswith('_')

# If the user defined any additional enum values in the template base class, remove them from the base and add
# them back using extend_enum() so they are usable enum values. For example:
# @enum_bitmask(MyEnum)
# class MyMask:
# ALL = 0xFF
for entry in inspect.getmembers(base_cls):
if entry[0] not in internals:
if _is_enum_entry(entry):
delattr(base_cls, entry[0])
extend_enum(WrappedCls, entry[0], int(entry[1]))

# Now define additional enum values for each bit:
# A = (1 << MyEnum.A)
if define_bits:
for entry in inspect.getmembers(enum_type):
if entry[0] not in internals and predicate(entry[1]):
if _is_enum_entry(entry) and predicate(entry[1]):
extend_enum(WrappedCls, entry[0], (1 << (int(entry[1]) - WrappedCls._enum_offset)))

return WrappedCls
Expand Down

0 comments on commit 5664946

Please sign in to comment.