Skip to content

Commit

Permalink
stdlib: add support for bare const
Browse files Browse the repository at this point in the history
Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
  • Loading branch information
Josverl committed Oct 9, 2023
1 parent b286fb6 commit f5a9d2b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions stubs/micropython-core/__builtins__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# allows for type checking of additional builtins by pyright

from typing import Tuple, TypeVar

Const_T = TypeVar("Const_T", int, float, str, bytes, Tuple) # constant

def const(expr: Const_T) -> Const_T:
"""
Used to declare that the expression is a constant so that the compiler can
optimise it. The use of this function should be as follows::
from micropython import const
CONST_X = const(123)
CONST_Y = const(2 * CONST_X + 1)
Constants declared this way are still accessible as global variables from
outside the module they are declared in. On the other hand, if a constant
begins with an underscore then it is hidden, it is not available as a global
variable, and does not take up any memory during execution.
This `const` function is recognised directly by the MicroPython parser and is
provided as part of the :mod:`micropython` module mainly so that scripts can be
written which run under both CPython and MicroPython, by following the above
pattern.
"""
...

0 comments on commit f5a9d2b

Please sign in to comment.