-
Couldn't load subscription status.
- Fork 1.1k
python-stdlib/enum/enum.py: Add Enum class. #980
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
Conversation
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
Usage example::
from enum import Enum
class State(Enum):
Stop = 10
Run = 20
Ready = 30
state = State()
print("state:", State())
current_state = state.Stop
print("current_state:", current_state, state.key_from_value(current_state))
if current_state == state.Stop:
print(" Stop state")
if current_state != state.Ready:
print(" Not a Ready state")
print(" Run!")
current_state = state.Run
print("current_state:", current_state, state.key_from_value(current_state))
# some process
i = -1
while current_state != state.Ready:
i += 1
if state.is_value(i):
if state(i) == state.Ready:
current_state = state.Ready
print(".", end="")
print()
print("current_state:", current_state, state.key_from_value(current_state))
print("Done!")
Output is::
state: State({'Ready': 30, 'Stop': 10, 'Run': 20})
current_state: 10 State.Stop
Stop state
Not a Ready state
Run!
current_state: 20 State.Run
...............................
current_state: 30 State.Ready
Done!
Thanks for the contribution, this looks pretty good!
Did you implement this from scratch, or copy parts from CPython's implementation? I'm just wondering about licensing and copyright.
Can you please add the test to the CI, in tools/ci.sh inside the function ci_package_tests_run.
Did you implement this from scratch, or copy parts from CPython's implementation?
I just saw CPython Enum. It looks like incredible magic. :-)
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
I just saw CPython Enum. It looks like incredible magic. :-)
That doesn't really answer the question. Did you copy this implementation from CPython?
Also, please make sure the CI all passes, there's currently a failure.
| Did you implement this from scratch, or copy parts from CPython's implementation?
No, I didn't use CPython implementation.
It was inspired by @shariltumin Dot class from the Way to use dot notation to refer to states in a state machine #15694
and @njourdane enum() func from the Request for package: micropython-enum #269
2c994fe to
e70dd06
Compare
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
e70dd06 to
6ab2ebe
Compare
Should I squash commits?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to run this test under CPython 3.12.2 but it doesn't work, for many reasons. And it should run under CPython so we can test that the implementation of MicroPython's enum matches the CPython enum.
For example, enum does not exist in the enum CPython module. Which version of CPython were you testing against?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CPython cannot create enums in this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CPython requires a value in the constructor here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CPython doesn't allow such an argument to the constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CPython doesn't allow calling an enum.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CPython doesn't have key_from_value().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CPython doesn't allow inheriting enums from each other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CPython doesn't have __len__ on an enum.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CPython enums don't have keys/values/items methods.
jonnor
commented
Apr 25, 2025
There is a quite comprehensive set of unit-tests for enum available in CPython: https://github.com/python/cpython/blob/main/Lib/test/test_enum.py
With some exceptions like the tests using inspect, threading, pickle it seems possible to port most of them to MicroPython. That would give a very high degree of confidence that the implementation is conformant. And in the cases that one chooses to not be conformant, that can be documented with skipped tests.
I have successfully completed the task that requires the Enum class.
I don't plan to support this PR in the future.
You are welcome to continue working with this PR as you wish.
Thanks everyone.
Uh oh!
There was an error while loading. Please reload this page.
Docs in:
docs/library/enum.rst: Add Enum class. #16842
Usage example:
Output is:
EDITED:
Inspired by @shariltumin Dot class from the Way to use dot notation to refer to states in a state machine #15694
and @njourdane enum() func from the Request for package: micropython-enum #269