ethereum_types.enum
Support for enumerations.
_T¶
| 12 | _T = TypeVar("_T") |
|---|
_copy_type ¶
_UintEnumType ¶
class _UintEnumType:
__call__ ¶
| 22 | @_copy_type(EnumType.__call__) |
|---|
| 23 | @override |
|---|
def __call__(cls, value, *args, **kwargs):
| 24 | def __call__(cls, value, *args, **kwargs): # type: ignore[no-untyped-def] |
|---|---|
| 25 | # Allow syntax like `MyUintEnum(3)` instead of `MyUintEnum(Uint(3))`. |
| 26 | # Required because we implement the dunder methods in `Unsigned` as |
| 27 | # `Self(int + int)`. |
| 28 | return super().__call__(Uint(value), *args, **kwargs) |
UintEnum ¶
Base class for creating enumerated constants that are also subclasses of
Uint.
UintEnum is similar to IntEnum, but its members are subclasses of
Uint instead of int.
Note: Unlike IntEnum, UintEnum integer operations that would
result in a numeric value without a corresponding enumeration member raise
a ValueError instead of silently converting to a Uint.
>>> from ethereum_types.numeric import Uint
>>> from ethereum_types.enum import UintEnum
>>> class Color(UintEnum):
... RED = Uint(1)
... BLUE = Uint(2)
...
>>> Color.RED
<Color.RED: 1>
class UintEnum:
__repr__¶
| 58 | __repr__ = Enum.__repr__ |
|---|
_UintFlagType ¶
class _UintFlagType:
_boundary_¶
| 62 | _boundary_: FlagBoundary |
|---|
__call__ ¶
| 64 | @_copy_type(EnumType.__call__) |
|---|
| 65 | @override |
|---|
def __call__(cls, value, *args, **kwargs):
| 66 | def __call__(cls, value, *args, **kwargs): # type: ignore[no-untyped-def] |
|---|---|
| 67 | result = super().__call__(value, *args, **kwargs) |
| 68 | if isinstance(result, int): |
| 69 | assert cls._boundary_ is not KEEP |
| 70 | value = result |
| 71 | else: |
| 72 | if cls._boundary_ is KEEP or result._name_ in cls.__members__: |
| 73 | return result |
| 74 | value = result._value_ |
| 75 | |
| 76 | if cls._boundary_ is FlagBoundary.EJECT: |
| 77 | return Uint(value) |
| 78 | |
| 79 | if value == 0: |
| 80 | member: UintFlag = Uint.__new__(cls) # type: ignore[arg-type] |
| 81 | Uint.__init__(member, 0) |
| 82 | member._value_ = 0 |
| 83 | member._name_ = "None" |
| 84 | return member |
| 85 | |
| 86 | return result |
| 87 | |
| 88 | assert_never(cls._boundary_) # pragma: nocover |
UintFlag ¶
class UintFlag:
__new__ ¶
_missing_ ¶
| 98 | @classmethod |
|---|
| 99 | @override |
|---|
def _missing_(cls, value: object) -> Union[None, "Uint", int]:
| 101 | if not isinstance(value, SupportsInt): |
|---|---|
| 102 | return None |
| 103 | |
| 104 | int_value = int(value) |
| 105 | missed = super()._missing_(int_value) |
| 106 | if isinstance(missed, UintFlag): |
| 107 | UintFlag.__init__(missed, int_value) |
| 108 | return missed |
| 109 | |
| 110 | assert isinstance(missed, (Uint, int)) |
| 111 | return missed |