Repository navigation
python-stdlib/enum/enum.py: Add Enum class. - #980
IhorNehrutsa wants to merge 4 commits into
Conversation
|
Usage example:: Output is:: |
|
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 |
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 |
e70dd06 to
6ab2ebe
Compare
|
Should I squash commits? |
| @@ -0,0 +1,91 @@ | |||
| # enum_test.py | |||
|
|
|||
| from enum import Enum, enum | |||
There was a problem hiding this comment.
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.
Version 1.4 matches CPython as I could manage.
| print("type(state('CW')):", type(state("CW"))) | ||
|
|
||
| print("state.key_from_value(20):", state.key_from_value(20)) | ||
| print("len(state):", len(state)) |
There was a problem hiding this comment.
CPython doesn't have __len__ on an enum.
There was a problem hiding this comment.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
print(dir(Color))
['GREEN', 'RED', '__class__', '__contains__', '__doc__', '__getitem__', '__init_subclass__', '__iter__',
'__len__',
'__members__', '__module__', '__name__', '__qualname__']
|
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 |
|
I have successfully completed the task that requires the Enum class. |
We need to mip install `datetime`, tzif-parser requires it. https://github.com/micropython/micropython-lib/tree/master/python-stdlib/datetime ``` mpremote connect id:a5f14b8bfbff4289 mip install datetime ``` ## CPython features originally used in `tzif-parser` which were missing or different in MicroPython * `sysconfig` * `enum` - micropython/micropython-lib#269 - micropython/micropython-lib#980 * Regex _named_ Groups - https://docs.python.org/3/howto/regex.html#non-capturing-and-named-groups - https://docs.micropython.org/en/latest/library/re.html * `dataclasses` - https://docs.python.org/3/library/dataclasses.html - https://github.com/orgs/micropython/discussions/13741 - https://github.com/dhrosa/udataclasses - https://udataclasses.readthedocs.io/en/latest/ * `struct` (https://docs.python.org/3/library/struct.html#format-characters) features not available in MicroPython (https://docs.micropython.org/en/latest/library/struct.html#module-struct): - `c` for `char`, which in CPython would be decoded as an instance of [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes-objects) with length 1. Instead of `c`, we could use `s`, but we know this is a single byte, so `b` does just fine, giving us a Python integer. The integral repeat count prefix `1` is superfluous. * [MicroPython `bytearray`](https://www.fredscave.com/43-micropython-data-types-bytearray.html) type doesn't have the [`clear()`](https://docs.python.org/3/library/stdtypes.html#sequence.clear) method [mutable sequences](https://docs.python.org/3/library/stdtypes.html#typesseq-mutable) in CPython do * `Typing.IO` - python/typing#829
We need to mip install `datetime`, tzif-parser requires it. https://github.com/micropython/micropython-lib/tree/master/python-stdlib/datetime ``` mpremote connect id:a5f14b8bfbff4289 mip install datetime ``` ## CPython features originally used in `tzif-parser` which were missing or different in MicroPython * `sysconfig` * `enum` - micropython/micropython-lib#269 - micropython/micropython-lib#980 * Regex _named_ Groups - https://docs.python.org/3/howto/regex.html#non-capturing-and-named-groups - https://docs.micropython.org/en/latest/library/re.html * `dataclasses` - https://docs.python.org/3/library/dataclasses.html - https://github.com/orgs/micropython/discussions/13741 - https://github.com/dhrosa/udataclasses - https://udataclasses.readthedocs.io/en/latest/ * `struct` (https://docs.python.org/3/library/struct.html#format-characters) features not available in MicroPython (https://docs.micropython.org/en/latest/library/struct.html#module-struct): - `c` for `char`, which in CPython would be decoded as an instance of [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes-objects) with length 1. Instead of `c`, we could use `s`, but we know this is a single byte, so `b` does just fine, giving us a Python integer. The integral repeat count prefix `1` is superfluous. * [MicroPython `bytearray`](https://www.fredscave.com/43-micropython-data-types-bytearray.html) type doesn't have the [`clear()`](https://docs.python.org/3/library/stdtypes.html#sequence.clear) method [mutable sequences](https://docs.python.org/3/library/stdtypes.html#typesseq-mutable) in CPython do * `Typing.IO` - python/typing#829
|
@dpgeorge Need some help on this PR ? Were could I start ? Dominique |
That would be much appreciated. |
|
@IhorNehrutsa, may I ask why you closed the documentation PR? |
This has been going on for so long that I forgot. :( |
|
I'm a bit skeptical about the last @IhorNehrutsa action. On the meanwhile, @IhorNehrutsa pushed an AI rework of the code and "here is it for you to test". Nevertheless, I do not want to be retrograde but being the "human behind the IA for checking the IA work" is not really what I was expecting. |
|
@mchobby, @IhorNehrutsa , I’m also genuinely confused by the pattern of behavior around these PRs: starting work, abandoning it, then suddenly re‑engaging with AI‑generated updates, along with the closing, and re‑opening of related PRs. It makes it difficult to understand the actual intent, the direction of the contribution, or whether you are committed to following through. There is clear interest in having an At the same time, it’s important that MicroPython stays close to CPython where feasible, while still respecting MicroPython’s own design principles and constraints. Achieving that balance requires careful design and discussion, not a surface‑level or (mostly) AI‑generated port. I have not had the time to look at the most recent tests and code - but I do plan to do that in the next few days. |
|
Okay. No one minds if I copy the previous version of enum.py to enum_0.py, the latest to enum_1.py, then we'll choose the best implementation options for enum.py from them. |
Can someone write a MicroPython version of the CPython tests https://github.com/python/cpython/blob/main/Lib/test/test_enum.py ? |
|
One of the twenty-something post its on my MicroPython to-do list is to do a full review and comparison. But I can't give you a date |
agatti
left a comment
There was a problem hiding this comment.
I've had a quick look at the code, and whilst I'm still going through the internals of this module, I can at least provide a couple of hints on how to make the final output smaller :)
|
Hi all, I know you're busy, but just gently bringing this back to your attention. Please let me know if any changes, additional tests, or documentation updates are required on my end. Appreciate your work. |
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
e5bcc1b to
30c120b
Compare
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
Docs in:
docs/library/enum.rst: Add Enum class. #16842
Usage example:
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
EDITED October 2026:
See
README.md, `CPy/CPy.py'Standard Behavior (Compatible)
Color.RED,.name,.valuefor item in Color()IntEnumandStrEnumtypes with strict value checkingEnum('Name', {'KEY': value, ...})Non-Standard Behavior
Key differences from CPython:
Color()) to trigger lazy member initialization when standard class bodies aren't fully traversed upfront.Color("RED")works for both name and value lookup (CPython standard call only supports value lookup).Color.RED == 1isTrue(only true forIntEnumin CPython)..dump()does not exist in stdlibEnum.Color.RED()returns the value (not supported in CPython).__len__:len(Color())works; CPython enums are not containers.dump()for eval-based reconstruction is MicroPython-specific.Comparing the implementations
enum.pyenum_mini.pyEnum,IntEnum,StrEnumEnum,IntEnum,StrEnumEnumColor["RED"]Color("RED")orColor()["RED"]Color("RED")Color(1)Color(1)Color(1)for item in Colorfor item in Color()for item in Color()__members____members__keys and valuesstartfor generated integer values.dump()Enummember equals its raw valueCode size report: enum.py
esp32: +2288 +0.126% ESP32_GENERIC[incl +2288(data)]
mimxrt: +2232 +0.567% TEENSY40
rp2: +2228 +0.231% RPI_PICO_W
samd: +2236 +0.804% ADAFRUIT_ITSYBITSY_M4_EXPRESS
Code size report: enum_mini.py
esp32: +1264 +0.070% ESP32_GENERIC[incl +1264(data)]
mimxrt: +1232 +0.313% TEENSY40
rp2: +1236 +0.128% RPI_PICO_W
samd: +1232 +0.443% ADAFRUIT_ITSYBITSY_M4_EXPRESS