Skip to content

Instantly share code, notes, and snippets.

@Josverl
Last active April 16, 2024 12:19
Show Gist options
  • Save Josverl/76e65c96c0c9d43b9c54031fa1e4aa24 to your computer and use it in GitHub Desktop.
Save Josverl/76e65c96c0c9d43b9c54031fa1e4aa24 to your computer and use it in GitHub Desktop.
DIY Stub file creation for MicroPython

DIY Stub file creation for MicroPython

To create stubs for your MicroPython board, you can use the micropython-stubber package. This package will connect to your board and create a set of stub files that you can use in your IDE to get code completion and type checking.

The recomnedation is:

  • To create a folder from which to run the tool and store the various repos and files
  • optionally to create a .venv virtual environment to run the tool in.

install micropython-stubber

pip install micropython-stubber

create a folder (and a pyproject.toml file )

This folder is where stubber will store the various files and repos Check the configuration with stubber show-config

14:12:36 | INFO     | cli                - micropython-stubber 1.19.0
14:12:36 | INFO     | config_cmd         - config file                D:\test\pyproject.toml
14:12:36 | INFO     | config_cmd         - CONFIG.repo_path           repos
14:12:36 | INFO     | config_cmd         - CONFIG.mpy_path            repos\micropython
14:12:36 | INFO     | config_cmd         - CONFIG.mpy_lib_path        repos\micropython-lib
14:12:36 | INFO     | config_cmd         - CONFIG.mpy_stubs_path      repos\micropython-stubs
14:12:36 | INFO     | config_cmd         - CONFIG.stub_path           repos\micropython-stubs\stubs
14:12:36 | INFO     | config_cmd         - CONFIG.publish_path        repos\micropython-stubs\publish
14:12:36 | INFO     | config_cmd         - CONFIG.template_path       repos\micropython-stubs\publish\template

clone the micropython source repos and the micropython-stubs repos

In order to create and merge the mcu-stub to a complete stub package, the stubber needs the micropython source code and the micropython-stubs repo.

Run: stubber clone --add-stubs

run the mcu stubber

Note that this will attempt to stub all connected boards.
TODO: Currently there is no option to specify a single board/serial port , but that would be useful

stubber get-mcu-stubs
14:07:41 | INFO     | cli                - micropython-stubber 1.19.0
14:07:42|SUCCESS |mcu_stubber          - Detected board Wio Terminal D51R with SAMD51P19A 1.22.2
┏━━━━━━━━━━━━━┳━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┓
┃ Serial Port ┃ Port ┃ Description                       ┃ Version ┃
┡━━━━━━━━━━━━━╇━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━┩
│ COM8        │ samd │ Wio Terminal D51R with SAMD51P19A │ 1.22.2  │
└─────────────┴──────┴───────────────────────────────────┴─────────┘
14:07:42|INFO    |mcu_stubber          - Connecting using COM8 to samd SEEED_WIO_TERMINAL 1.22.2: Wio Terminal D51R with SAMD51P19A
14:07:45|INFO    |runner               - Install github:josverl/micropython-stubber/mip/mpy_v6.json
14:07:45|INFO    |runner               - Installing github:josverl/micropython-stubber/mip/mpy_v6.json to /lib
14:07:45|INFO    |runner               - Installing: /lib/createstubs.mpy
14:07:45|INFO    |runner               - Installing: /lib/createstubs_mem.mpy
14:07:45|INFO    |runner               - Installing: /lib/createstubs_db.mpy
14:07:45|INFO    |runner               - Installing: /lib/modulelist.txt
14:07:45|INFO    |runner               - Done
14:07:45|INFO    |mcu_stubber          - Writing BOARDNAME='SEEED_WIO_TERMINAL' to boardname.py
14:07:46|INFO    |mcu_stubber          - Resetting COM8 Wio Terminal D51R with SAMD51P19A
14:07:48|INFO    |mcu_stubber          - Running createstubs db on COM8 Wio Terminal D51R with SAMD51P19A using temp path: ...board_stubber0dy6kl44
14:07:48|INFO    |mcu_stubber          - Running : mpremote mount ..board_stubber0dy6kl44 exec import createstubs_db
14:07:49|INFO    |runner               - Starting new run
14:07:49|INFO    |runner               - Found BOARDNAME: SEEED_WIO_TERMINAL
14:07:49|INFO    |runner               - Port: samd
14:07:49|INFO    |runner               - Board: SEEED_WIO_TERMINAL
14:07:49|INFO    |runner               - Clean/remove files in folder: /remote/stubs/micropython-v1_22_2-samd-SEEED_WIO_TERMINAL
14:07:49|INFO    |runner               - Report file: /remote/stubs/micropython-v1_22_2-samd-SEEED_WIO_TERMINAL/modules.json
14:07:49|INFO    |runner               - Stub module: _asyncio                  to file: _asyncio.pyi                                                           mem:147120
14:07:50|INFO    |runner               - Stub module: _onewire                  to file: _onewire.pyi                                                           mem:147120
14:07:53|INFO    |runner               - Stub module: array                     to file: array.pyi                                                              mem:146272
...
...
14:08:35|INFO    |runner               - Stub module: usys                      to file: usys.pyi                                                               mem:132784
14:08:36|INFO    |runner               - Stub module: utime                     to file: utime.pyi                                                              mem:132752
14:08:37|INFO    |runner               - All modules have been processed, Finalizing report
14:08:37|INFO    |runner               - Path: /remote/stubs/micropython-v1_22_2-samd-SEEED_WIO_TERMINAL
14:08:37|INFO    |runner               - Local directory ...board_stubber0dy6kl44 is mounted at /remote
14:08:39|INFO    |post                 - Running autoflake on: ...board_stubber0dy6kl44\stubs\micropython-v1_22_2-samd-SEEED_WIO_TERMINAL
14:08:39|SUCCESS |mcu_stubber          - Stubs generated for samd-SEEED_WIO_TERMINAL
14:08:39|SUCCESS |mcu_stubber          - Stubs copied to repos\micropython-stubs\stubs\micropython-v1_22_2-samd-SEEED_WIO_TERMINAL
14:08:39|INFO    |mcu_stubber          - Merging stubs with docstubs : {'build': '', 'ver': '1.22.2', 'version': '1.22.2', 'port': 'samd', 'board': 'SEEED_WIO_TERMINAL', 'mpy': 'v6.2', 'family': 'micropython', 'cpu': 'SAMD51P19A', 'arch': 'armv7emsp'}
14:08:39|INFO    |merge_docstubs       - checking 1 possible board candidates
14:08:39|INFO    |merge_docstubs       - Merge v1.22.2 docstubs with boardstubs to micropython-v1_22_2-samd-SEEED_WIO_TERMINAL-merged
14:08:49|INFO    |merge_docstubs       - merged 1 of 1 candidates
14:08:49|INFO    |mcu_stubber          - Building package for {'build': '', 'ver': '1.22.2', 'version': '1.22.2', 'port': 'samd', 'board': 'SEEED_WIO_TERMINAL', 'mpy': 'v6.2', 'family': 'micropython', 'cpu': 'SAMD51P19A', 'arch': 'armv7emsp'}
14:08:49|INFO    |publish              - checking 1 possible board candidates
14:08:49|INFO    |stubpackage          - Build: micropython-v1_22_2-samd-seeed_wio_terminal-stubs
14:08:49|INFO    |stubpackage          - - Update micropython-v1_22_2-samd-seeed_wio_terminal-stubs
14:08:56|INFO    |stubpackage          - Found changes to package sources: micropython-samd-seeed_wio_terminal-stubs 1.22.2.post1 
                                                        Results
┏━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━┓
┃ Result   ┃ Name/Path                                                                         ┃ Version      ┃ Error ┃
┡━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━┩
│ Build OK │ micropython-samd-seeed_wio_terminal-stubs                                         │ 1.22.2.post1 │       │
│          │ repos/micropython-stubs/publish/micropython-v1_22_2-samd-seeed_wio_terminal-stubs │              │       │
└──────────┴───────────────────────────────────────────────────────────────────────────────────┴──────────────┴───────┘
14:08:59|SUCCESS |mcu_stubber          - Done

check results

You should then find the prepared stubs package under the micropython-stubs/publish folder .repos/micropython-stubs/publish/micropython-v1_22_2-samd-seeed_wio_terminal-stubs

The folder naming convention is micropython-<flat_version>-<port>-<BOARD_ID>-stubs

Install the stubs to a typing folder to use in your IDE

pip install repos/micropython-stubs/publish/micropython-v1_22_2-samd-seeed_wio_terminal-stubs --target ./typings --no-user

Create a PR to the micropython-stubs repo

If you have added stubs for a board or version that is not yet in the micropython-stubs repo, please create a PR to the repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment