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 pytest mpflash 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, wether 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 VSCode Python extensions
  3. Specific configuration of Pylint [ Optional ]
  4. Suppression of warnings that collide with the MicroPython principals 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. A second repo micropy-stubs repo maintained by BradenM, also contains stubs, but in a structure only used and distributed by the micropy-cli tool. You should use micropy-cli to consume stubs from that repo.

The (stretch) goal is to create a VSCode add-in to simplify the configuration, and allow easy switching between different firmwares and versions.

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.18 )
stubber get-docstubs

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

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

# Update the fallback stubs
stubber update-fallback

#
ls all-stubs
dir all-stubs

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

Both MicroPython-Stubber and mpflash are 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

๐Ÿ’ป
shariltumin
shariltumin

๐Ÿ’ฅ โœ…
Matt Trentini
Matt Trentini

๐Ÿ’ฅ โœ…
Stewart Russell
Stewart Russell

๐Ÿ’ฅ โœ…
Andrew Leech
Andrew Leech

๐Ÿ’ฅ โœ…
Wouter van Ooijen
Wouter van Ooijen

๐Ÿ’ฅ โœ…
Shane Powell
Shane Powell

๐Ÿ’ฅ โœ…
Robert Hammelrath
Robert Hammelrath

๐Ÿ’ฅ โœ…
Bg
Bg

๐Ÿ’ฅ โœ…
Raul KompaรŸ
Raul KompaรŸ

๐Ÿ’ฅ โœ…
garryp4
garryp4

๐Ÿ’ฅ โœ…
Shane Powell
Shane Powell

๐Ÿ’ฅ โœ…
Andy Piper
Andy Piper

๐Ÿ’ฅ โœ…
David Horton
David Horton

๐Ÿ’ฅ โœ…

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 Distribution

micropython_stubber-1.23.3.tar.gz (314.8 kB view details)

Uploaded Source

Built Distribution

micropython_stubber-1.23.3-py3-none-any.whl (376.5 kB view details)

Uploaded Python 3

File details

Details for the file micropython_stubber-1.23.3.tar.gz.

File metadata

  • Download URL: micropython_stubber-1.23.3.tar.gz
  • Upload date:
  • Size: 314.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Windows/10

File hashes

Hashes for micropython_stubber-1.23.3.tar.gz
Algorithm Hash digest
SHA256 b291bb847ff82f9fd58b89e4ff79c7a275fe21754a778bc1a4049a9c4843a70d
MD5 a7494dce2b8cc8e9eddf7446e9a31a44
BLAKE2b-256 eec706f1ff8df109360cde2bca3e8ce9f83aacf2043e3ff1c9250c861f2ebfb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for micropython_stubber-1.23.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5f22a5cbcf7ffe2729ae6f99b7f9dcfe95a93245784fba9265623c824dc0efc4
MD5 add7c13329517bfa0511f12ba66457d7
BLAKE2b-256 1fe35d45311a83ad0d98d519e041c2ca7d0fc71a1c6327dcde1ca3350f275ea5

See more details on using hashes here.

Supported by

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