← index #17453Issue #18811
Off-topic · high · value 2.396
QUERY · ISSUE

micropython.const inconsistent behaviour in class

openby konomikittenopened 2025-06-07updated 2025-06-08
bug

Port, board and/or hardware

Waveshare ESP32-C3-Zero Development Board

MicroPython version

MicroPython v1.25.0 on 2025-04-15; ESP32C3 module with ESP32C3

Reproduction

with_const.py

from micropython import const

class HelloWorld:
    TEST = const("Hello World!")

    def __init__(self):
        print(TEST)
>>> import with_const
>>> hello_world = with_const.HelloWorld()
Hello World!
>>>

without_const.py

class HelloWorld:
    TEST = "Hello World!"

    def __init__(self):
        print(TEST)
>>> import without_const
>>> hello_world = without_const.HelloWorld()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "without_const.py", line 5, in __init__
NameError: name 'TEST' isn't defined
>>>

I wouldn't have noticed that I wasn't properly referencing my class constant variable if it wasn't for PyLint telling me I was making mistakes. This seems like a bug?

Expected behaviour

I expected the example using micropython.const() to error.

Observed behaviour

There is no error.

Additional Information

No, I've provided everything above.

Code of Conduct

Yes, I agree

CANDIDATE · ISSUE

RP2 asyncio stream IO hangs: regression

openby peterhinchopened 2026-02-13updated 2026-03-18
bugport-rp2

Port, board and/or hardware

RP2040 or RP2350

MicroPython version

Fails on release builds >= 22.0

Bug seems RP2-specific: the script runs on STM32 (Pyboard 1.1) and ESP32-S3 with current firmware (1.27).

Reproduction

Either paste the script at the REPL or run from a file foo.py (mpremote mount . "exec import foo")

import asyncio # as asyncio
import time
import io
MP_STREAM_POLL_RD = const(1)
MP_STREAM_POLL = const(3)
MP_STREAM_ERROR = const(-1)

class MillisecTimer(io.IOBase):
    def __init__(self):
        self.end = 0
        self.sreader = asyncio.StreamReader(self)

    def __iter__(self):
        await self.sreader.read(1)

    def __call__(self, ms):
        self.end = time.ticks_add(time.ticks_ms(), ms)
        return self

    def read(self, _):
        return b"a"

    def ioctl(self, req, arg):
        ret = MP_STREAM_ERROR
        #time.sleep_us(2_000)
        if req == MP_STREAM_POLL:  # hangs HERE
            ret = 0
            if arg & MP_STREAM_POLL_RD:
                if time.ticks_diff(time.ticks_ms(), self.end) >= 0:
                    ret |= MP_STREAM_POLL_RD
        return ret

async def timer_test(n):
    timer = MillisecTimer()
    for x in range(n):
        await timer(100)  # Pause 100ms
        print(x)

asyncio.run(timer_test(20))

Expected behaviour

Expected to print integers from 0 to 19.

Observed behaviour

Hangs on the line commented with "hangs HERE".

Behaviour can be "fixed" by uncommenting the 2ms delay.

Additional Information

No, I've provided everything above.

Code of Conduct

Yes, I agree

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