-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request for package: micropython-enum #269
Comments
Enums are especially useful when it comes to static type-checking. So it would be great if the implementation of this enum type allowed type-checking via mypy. This means all values need to be an instance of the defined enum-type.
|
I'm also interested by enum in MicroPython. In the meantime, I found here this workaround: def enum(**enums: int):
return type('Enum', (), enums)
Number = enum(ONE=1, TWO=2, THREE=3)
numbers = (Number.ONE, Number.TWO) |
for type-checking, I'm using the following trick: if TYPE_CHECKING:
from enum import IntEnum
else:
IntEnum = object
class Fruit(IntEnum):
APPLE = 1
PEAR = 2
def print_fruit(fruit: Fruit):
if fruit == Fruit.APPLE:
print("apple")
print_fruit(Fruit.APPLE) |
This workaround is excellent! I combined it with const for my purposes:
|
+1 for a real implementation of this |
class State(int):
pass
State.OFF = State(0)
State.ON = State(1) But when have been imported getting "Unresolved attribute reference" warning. |
The cpython implementation is surprisingly very complicated and relies heavily on metaclasses which aren't supported in micropython: As such any micropython implementation will need to be completely custom really, at which point we need to clearly define which aspects of Enum we want to support first, before figuring out how to implement them! |
There are a number of limitations when subclassing builtins in micropython, especially |
+1 for an implementation of this as well. |
I would like micropython to have support for
IntEnum
class. BaseEnum
would be nice to have, although just bareIntEnum
would be enough in my opinion.The text was updated successfully, but these errors were encountered: