← index #269Issue #8795
Off-topic · high · value 0.713
QUERY · ISSUE

Request for package: micropython-enum

openby desowinopened 2018-04-04updated 2025-03-05
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.

11 comments
stlehmann · 2018-04-04

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.

>>> class MyEnum(enum.IntEnum):
...    A = 1
...    B = 2

>>> isinstance(MyEnum.A, MyEnum)
True
lypwig · 2022-02-20

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)
matejcik · 2022-02-20

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)
brotherdust · 2022-10-14

This workaround is excellent! I combined it with const for my purposes:

OP_RW = enum(
        READ = const(0b1),
        WRITE = const(0b1)
    )
esologic · 2022-12-11

+1 for a real implementation of this

i33l · 2023-05-26
class State(int):
    pass

State.OFF = State(0)
State.ON = State(1)

But when have been imported getting "Unresolved attribute reference" warning.

andrewleech · 2023-05-26

+1 for a real implementation of this

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!

andrewleech · 2023-05-26
class State(int):
    pass

State.OFF = State(0)
State.ON = State(1)

But when have been imported getting "Unresolved attribute reference" warning.

There are a number of limitations when subclassing builtins in micropython, especially int because 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

bredbord · 2024-03-07

+1 for an implementation of this as well.

CANDIDATE · ISSUE

RFC: Move forums to GitHub Discussions

closedby jimmoopened 2022-06-21updated 2022-12-13
enhancement

The forums continue to run well on phpbb and I am hesitant to mess with something that is working, but there are definitely a few things I'd like to improve. We looked at migrating to Discourse, which I think would be an overall improvement over phpbb, but recently GitHub have added "Discussions" support, and I think this might be a better direction.

  • Keep everything in one place (GitHub) for code, issues, PRs, and now discussion.
  • Much more modern editing (including emoji response, threads, accepted answer, etc) and markup tools (code formatting, etc).
  • Many users already have GitHub accounts, whereas current creating a forum.micropython.org account is an extra step (also it's not possible for new users to create new threads on the existing forum until they reply to some other threads).
  • Better support for notifications and subscriptions.
  • Don't need to run/maintain our own server.

The modern editing/markup tools are a big factor for me. It may sound a bit trivial but the emoji response would be really nice just to judge engagement, and the accepted answer feature would make it easier for people to make use of previous threads. There's also category support (just like phpbb) but also tagging.

Downsides:

  • There's no automatic way to import history, so we'd need to leave the current forum running in read-only mode. (FWIW, this was also an issue with moving to Discourse, there's a phpbb importer but it had issues). There is a huge wealth of information in the forums. (It looks like it should be possible to use the GitHub API to import thread content, although all the posts would be from a robot account but we could annotate the message text with the original author).
  • No way to import users, so people would need to create GitHub accounts to post/reply (this is really no different to the current forum, so this really only impacts the (small number of?) people who have forum accounts but not GitHub accounts).
  • More dependence on a third-party service (GitHub).

Here's some example projects currently using GitHub notifications:

  • https://github.com/home-assistant/frontend/discussions/
  • https://github.com/nodejs/node/discussions
  • https://github.com/faster-cpython/ideas/discussions

More info here: https://docs.github.com/en/discussions

I propose that we could create an organisation-level discussion (conceptually, spanning micropython and micropython-lib).

Keen to hear what people think! cc @peterhinch @robert-hh @davehylands @stinos @kevinkk525 @mcauser @mattytrentini @scruss @projectgus (and @dpgeorge )

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied