QUERY · ISSUE
Request for package: micropython-enum
enhancement
I would like micropython to have support for IntEnum class. Base Enum would be nice to have, although just bare IntEnum would be enough in my opinion.
CANDIDATE · ISSUE
Enum for micropython
enhancement
There is native enum implementation for micropython?
Using package of backported enum34:
>>> import enum
Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x400e2314 PS : 0x00060430 A0 : 0x800e0280 A1 : 0x3ffccad0
A2 : 0x400e09a0 A3 : 0x00000018 A4 : 0x3ffe7b14 A5 : 0x0c008136
A6 : 0x00000407 A7 : 0x3ffe4db0 A8 : 0x00000019 A9 : 0x3ffccab0
A10 : 0x0c008136 A11 : 0x00000000 A12 : 0x00000010 A13 : 0x3ffe8e30
A14 : 0x00000030 A15 : 0x00000001 SAR : 0x0000001a EXCCAUSE: 0x0000001c
EXCVADDR: 0x0c008152 LBEG : 0x4000c46c LEND : 0x4000c477 LCOUNT : 0x00000000
Backtrace:0x400e2311:0x3ffccad0 0x400e027d:0x3ffccaf0 0x400e0339:0x3ffccb20 0x400e231e:0x3ffccb60 0x400e2746:0x3ffccb80 0x40083a91:0x3ffccba0 0x400dbc20:0x3ffccc40 0x400e2589:0x3ffccc70 0x400e0c22:0x3ffccc90 0x400e08fe:0x3ffcccf0 0x400e2589:0x3ffccd10 0x40084559:0x3ffccd30 0x400dbc20:0x3ffccdd0 0x400e2589:0x3ffcce40 0x400e262c:0x3ffcce60 0x400fe96d:0x3ffccef0 0x400febc6:0x3ffccf20 0x400e2734:0x3ffcd000 0x4008482d:0x3ffcd040 0x400dbc20:0x3ffcd0e0 0x400e2589:0x3ffcd130 0x400e25b2:0x3ffcd150 0x400ef916:0x3ffcd170 0x400efaa9:0x3ffcd200 0x400d4cd6:0x3ffcd250
ELF file SHA256: 0042e5dc6a30789f
Rebooting...
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4
load:0x3fff0034,len:4536
load:0x40078000,len:12344
load:0x40080400,len:4124
entry 0x40080680
MicroPython v1.18 on 2022-04-14; ESP32 module with ESP32
Type "help()" for more information.
Than was tried another way to mpy-cross CPython libs enum.py and type.py:
>>> import enum
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "./py/enum.py", line 3, in <module>
File "./py/types.py", line 14, in <module>
AttributeError: 'function' object has no attribute '__code__'
Related topic was found: https://github.com/micropython/micropython/issues/2084
Any idea how to use emun in micropython? Is required by python-chess that I'm trying to use
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:
for type-checking, I'm using the following trick:
This workaround is excellent! I combined it with const for my purposes:
+1 for a real implementation of this
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:
https://github.com/python/cpython/blob/f585ed19ad00f78ed99ba44be5e333c056076160/Lib/enum.py#L1051
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
intbecause behind the scenes it's particularly optimised. Eg. https://docs.micropython.org/en/latest/genrst/builtin_types.html#no-int-conversion-for-int-derived-types-available+1 for an implementation of this as well.
docs/library/enum.rst: Add Enum class. #16842
Implementation in:
micropython-lib/python-stdlib/enum/enum.py: Add Enum class. #980