Skip to main content

Tooling to create and maintain stubs for MicroPython

Project description

Boost MicroPython productivity in VSCode

pypi version python versions Black Star on GitHub All Contributors

pytest stubber Documentation Status

Open in VSCode

The IntelliSense and code linting that is so prevalent in modern editors, does not work out-of-the-gate for MicroPython projects. While the language is Python, the modules used are different from CPython, and also different ports have different modules and classes, or the same class with different parameters.

Writing MicroPython code in a modern editor should not need to involve keeping a browser open to check for the exact parameters to read a sensor, light-up a LED or send a network request.

Fortunately with some additional configuration and data, it is possible to make the editors understand your flavor of MicroPython, whether you use one of the pre-compiled firmwares, but also if you run a one-off custom firmware version.

demo]

In order to achieve this a few things are needed:

  1. Stub files for the native / enabled modules in the firmware using PEP 484 Type Hints
  2. Specific configuration of the VS Code Python extensions
  3. Specific configuration of Pylint [ Optional ]
  4. Suppression of warnings that collide with the MicroPython principles or code optimization.

Please review the documentation on [https://micropython-stubber.readthedocs.io]

With that in place, VSCode will understand MicroPython for the most part, and help you to write code, and catch more errors before deploying it to your board.

Note that the above is not limited to VSCode and pylint, but it happens to be the combination that I use.

A lot of subs have already been generated and are shared on PyPi, github or pre-installed by a tool, so it is quite likely that you can just grab a copy be be productive in a few minutes.

To install the stubs from PyPI

This section describes how to install the stubs from PyPI, and how to use them in your project. If you want to create or maintain stub - please see the next section.

  • Install in a typings folder (recommended) pip install -U micropython-<port>[-<board>]stubs --no-user --target ./typings
  • Install in a venv (after activating) pip install -U micropython-<port>[-<board>]stubs --no-user

Examples:

pip install -U micropython-stm32-stubs

# Install stubs for a specific version.
pip install -U micropython-esp32-stubs==1.20.0.*

# Install stubs for a specific board.
pip install -U micropython-rp2-pico_w-stubs

For more details how to use the stubs please refer to the documentation on RTD

  1. The sister-repo MicroPython-stubs contains all stubs I have collected with the help of others, and which can be used directly. That repo also contains examples configuration files that can be easily adopted to your setup.

  2. Micropython-stubber uses MPFlash in some of its operations. mpflash was formerly hosted in this repo, but has been moved to its own repo.

Install and basic usage of the stubber tool

pip install micropython-stubber

# go to your working folder 
cd my_stub_folder
mkdir all-stubs

# clone the micropython repo's and switch to a specific version 
stubber clone --add-stubs
stubber switch v1.22.2

# get the document stubs for the current version ( v1.22.2 )  
stubber docs-stubs

# get the frozen stubs for the current version ( v1.22.2 )
stubber frozen-stubs

# get the core CPython compatibility stubs from PyPi 
stubber get-core

# Update the fallback stubs
stubber update-fallback

#
ls all-stubs
dir all-stubs

Working with Custom MicroPython Builds

If you're working with a fork, branch, or custom build of MicroPython (such as a custom RPi Pico W firmware), see the Custom MicroPython Guide for detailed instructions on how to generate stubs for your specific build.

Developing & testing

This is described in more detail in the developing and testing documents in the docs folder.

Branch Main

The name of the default branch has been changed to main. If you have cloned this repo before you main need to adjust the local repro to be aware of this, or create a fresh clone.

To update run the below command:

git branch -m master main                    
git fetch origin
git branch -u origin/main main                      
git remote set-head origin -a

for more info see Renaming a branch

Licensing

MicroPython-Stubber is licensed under the MIT license, and all contributions should follow this LICENSE.

Contributions

Jos Verlinde
Jos Verlinde

💻 🔬 🤔 🖋 🖍️
Thonny, Python IDE for beginners
Thonny, Python IDE for beginners

🤔 🔬
MicroPython
MicroPython

🔣 🖍️
pyright
pyright

🔧
Boris Lovosevic
Boris Lovosevic

🔣 🖍️
Paul Sokolovsky
Paul Sokolovsky

🔣 🖍️
pycopy
pycopy

🔣 🖍️
Pycom
Pycom

🚇
Braden Mars
Braden Mars

🐛 💻 🖍️ 📦
James Manners
James Manners

💻 🐛
Patrick
Patrick

🐛 💻 🖍️
Paul m. p. P.
Paul m. p. P.

🤔 🔬
Edward K. Ream
Edward K. Ream

🔌
Daryl Stultz
Daryl Stultz

🖍️
Keeping things together
Keeping things together

🐛
vbolshakov
vbolshakov

🐛 🖍️
Mauro Riva
Mauro Riva

📝 🐛
MathijsNL
MathijsNL

🐛
Callum Jacob Hays
Callum Jacob Hays

🐛
Zoltán Vörös
Zoltán Vörös

🔣
vincent-l-j
vincent-l-j

📖
Egor Litvinov
Egor Litvinov

🐛
Sam Duke
Sam Duke

💻
Howard Lovatt
Howard Lovatt

🖍️ 🔬
VirtualWolf
VirtualWolf

Lukas Kremla
Lukas Kremla

This project follows the all-contributors specification. Contributions of any kind welcome!



Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

micropython_stubber-1.26.3-py3-none-any.whl (263.1 kB view details)

Uploaded Python 3

File details

Details for the file micropython_stubber-1.26.3-py3-none-any.whl.

File metadata

File hashes

Hashes for micropython_stubber-1.26.3-py3-none-any.whl
Algorithm Hash digest
SHA256 fbf10283aa5d6ae64d1fc5f0040009e3b7501a689641240e5e46e4dc10119a2d
MD5 7a8fb878fb956fd40b6fca3dbac5f422
BLAKE2b-256 0496a5cb8199558766e089fcec75f5186aad7cc2fd38d997977c77e6ff3e6280

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page