← index #17453PR #18714
Related · high · value 1.083
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 · PULL REQUEST

docs: Improve micropython.const() documentation and consistency

openby andrewleechopened 2026-01-22updated 2026-01-27
docs

Summary

Expanded micropython.const() documentation to address gaps identified through
investigation of GitHub issues and user confusion.

The current documentation lacks details about what const() actually does, has no
warnings about scope or import syntax requirements, and doesn't explain the
confusing behavior when used incorrectly. This has led to multiple GitHub
issues from confused users.

Changes include scope requirements, import syntax warnings, storage/visibility
explanation, and limitations section. Also fixed inconsistency in
speed_python.rst and added cross-references from other docs back to the main
micropython.const() documentation.

All issues reviewed against updated documentation to ensure complete coverage.

GitHub issues referenced:

  • https://github.com/micropython/micropython/issues/573
  • https://github.com/micropython/micropython/issues/11929
  • https://github.com/micropython/micropython/issues/15246
  • https://github.com/micropython/micropython/issues/15608
  • https://github.com/micropython/micropython/issues/17453

Testing

Built docs with Sphinx, no warnings or errors. Verified HTML rendering and
cross-reference links.

Trade-offs and Alternatives

The expanded documentation increases the micropython.const() function
documentation from ~18 to ~89 lines, making it much longer than other function
entries in the same file. An alternative would be to create a separate
documentation page (e.g. docs/reference/const.rst) with the detailed
explanation and keep only a brief summary with cross-reference in the library
reference. Feedback welcome on whether this restructuring would be preferred.

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