Skip to content

Commit 2b77a6f

Browse files
committed
Add a static stubs directory
Stubs are mostly generated from modules in shared-bindings. But in order to generate the stub the c code has to be commented with the appropriate type hints. If a module is located somewhere else (like micropython.const) and/or there are not the appropriate doc comments, no stub is generated for it. For these occassions we can create static stubs in the stubs directory that can be used in circuitpython-stubs. This also creates a stub for micropython.const and adds the static stubs directory into the makefile stubs command.
1 parent 473557c commit 2b77a6f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ stubs:
280280
sed -e "s,__version__,`python -msetuptools_scm`," < setup.py-stubs > circuitpython-stubs/setup.py
281281
cp README.rst-stubs circuitpython-stubs/README.rst
282282
cp MANIFEST.in-stubs circuitpython-stubs/MANIFEST.in
283+
cp -r stubs/* circuitpython-stubs
283284
$(PYTHON) tools/board_stubs/build_board_specific_stubs/board_stub_builder.py
284285
cp -r tools/board_stubs/circuitpython_setboard circuitpython-stubs/circuitpython_setboard
285286
$(PYTHON) -m build circuitpython-stubs

stubs/micropython/__init__.pyi

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Access and control MicroPython internals"""
2+
3+
from typing import TypeVar
4+
5+
T = TypeVar("T")
6+
7+
def const(expr: T) -> T:
8+
"""Used to declare that the expression is a constant so that the compiler
9+
can optimise it. The use of this function should be as follows:
10+
11+
from micropython import const
12+
13+
CONST_X = const(123)
14+
CONST_Y = const(2 * CONST_X + 1)
15+
16+
Constants declared this way are still accessible as global variables from
17+
outside the module they are declared in. On the other hand, if a constant
18+
begins with an underscore then it is hidden, it is not available as a global
19+
variable, and does not take up any memory during execution.
20+
21+
This const function is recognised directly by the MicroPython parser and is
22+
provided as part of the micropython module mainly so that scripts can be
23+
written which run under both CPython and MicroPython, by following the above
24+
pattern."""

0 commit comments

Comments
 (0)