Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Python Type Annotations: Tooling/Gaps/Pain Points#2316

yangdanny97 started this conversation inGeneral
Discussion options

Hi, I'm following up fromthis post in the Python Typing forum, where this project was mentioned as a potential area that could benefit from typing investments.

I wasn't able to find any local typechecking configurations or CI workflows, though I see some past discussions around this, for example in#1173

I'd like to learn more about the current state of things, and any difficulties or pain points maintainers have encountered while trying to use Python types.

Is there tooling or automation that you wish you had, which would make it easier to add types or stubs for this codebase & keep the updated?

Thanks!

You must be logged in to vote

Replies: 2 comments 8 replies

Comment options

There is an initial release for the webassembly port of MicroPython that including an initial stub for thepyscript module that is released to PyPI asmicropython-webassembly-stubs

https://github.com/Josverl/micropython-stubs/tree/main/publish/micropython-v1_24_1-webassembly-stubs

That is based on the MicroPython specific tools I created, and parttly stubs created bypyright --createstub

The challenge is to create processes and tools to maintain consistent stubs and documentation without increasing the efforts, or complexity to do so for all maintainers.

One thing that is missing specifically for micropython is that today it is not possible for type checkers to select on the MicroPython version, nor on the port (webassembly in this context)
So rather than maintain one set of stubs with conditions, multiple packages are needed, increasing the complexity for both maintenance and use

You must be logged in to vote
5 replies
@yangdanny97
Comment options

@Josverl

Thanks for the response.

The typing specification for Python mentionssystem and platform directives to select onsys.version andsys.platform

I think a similar feature forsys.implementation would allow you to select stubs specifically for micropython. The best that can be done today would be asys.platform == 'pyboard' check, but I see that the micropython docs mention that this won't always work.

For the other thing you mentioned "port (webassembly in this context)", how are they usually identified? I would have thought that it would be a different value forsys.platform.

@Josverl
Comment options

@yangdanny97,
I think the last time I looked at that I was looking for a way to distinguish on the micropython version , and I did not pay attention to thesys.platform bit, so thanks for making me read that once again.

sys.platform
On this sense micropython's 'port' does indeed equate tosys.platform, so that will help if that indeed can be used in typing and validation of typing.
Currently there are some 19 different ports of micropython of which ~12 are most activivly used (esp32, esp8266, mimxrt, nrf, renesas-ra, rp2, samd, stm32, unix, webassembly, windows, zephyr) across these there are some 180 different boards, that we can choose to ignore to avoid too much complexity.
I can start with testing that.

version(s) :sys.version ,sys.implementation.version
MicroPython's sys.implmentation.version (v1.24.1) is different from Python's version (v3.4.0 )

Connected to MicroPython at COM4Use Ctrl-] or Ctrl-x to exit this shellsys.platform'esp32'>>> sys.implementation(name='micropython', version=(1, 24, 1, ''), _machine='Generic ESP32 module with ESP32', _mpy=11014)>>> sys.version'3.4.0; MicroPython v1.24.1 on 2024-11-29'>>> sys.version_info(3, 4, 0)

This is due to the fact that MicroPython implements Python 3.4 and some select features of Python 3.5 and above.See

So it would be very useful when sys.impementation.version could be used in the same places as sys.version

@yangdanny97
Comment options

When you're using typecheckers on these stubs, is micropython the python interpreter in that environment? Or are you writing and checking the code on a computer with cpython & then later using it on a microcontroller with micropython?

If it's the latter, thensys.platform checks should work pretty well. Even if the runtime value differs, you can configure typecheckers to override thesys.platform value for typechecking purposes, so you could tell users to set their platform override to some specific value for micropython.https://mypy.readthedocs.io/en/stable/config_file.html#platform-configuration

It might be possible to add a typechecker directive to checksys.impementation.version (and maybesys.implementation.name too). I'm not sure if anyone's floated that idea before, but one of us could post in the Python typing forum about it.

@Josverl
Comment options

For MicroPython bare metal ports the type checking is done on the developers host / IDE or in CI using a 'fork' of _typeshed's stdlib that is tuned to remove functionality not available in MicroPython's stdlib, and add specifics such astime.sleep_ms
The vast majority of port/platform specifics are contained in platform specific modules (rp2,esp32,pyboard) and to a degree for the webassembly port/platform that ispyscript, but that separation is not 100% so using sys.platform could be useful

There are a few things that may hinder that though.
Typecheckers need simple checks
So will typecheckers be able to process inin expression such as

ifsys.platformin ["esp32"," esp8266"," mimxrt"," nrf"," renesas-ra"," rp2"," samd"," stm32"," unix"," webassembly"," windows"," zephy"]:@overloaddefsleep_ms(delay:float)->None: ...@overloaddefsleep_ms(delay:int)->None: ...

or should it be a longer but simplerif sys.platform == "esp32" or sys.platform == "esp8266" ..., repeating for all platforms ?

Specifically in stdlib the logic seems to be assuming 3 platforms : linux, darwin, win32 , and only these three.
There are quite a few parts where the logic is like : if sys.platform notlinux nordarwin , then it must bewin32 and variations thereof.

It does not seem to cause any problems in a cursory QA testrun, but when we add 12+ platforms to a set of 3 current, then I want to think this though.

@yangdanny97
Comment options

if sys.platform == "esp32" or sys.platform == "esp8266"

This would be better, I'm pretty sure mypy and pyright can both understand it, I don't think they can understand thein [...] syntax.

Comment options

on a slightly different tack

  • would it be useful to have sample configurations on how to setup static typechecking for MicroPython in CI
    or in a doctest to validate code samples ?
You must be logged in to vote
3 replies
@yangdanny97
Comment options

Yeah, I think that could be pretty useful. Some sort of starter templates for github actions, pre-commit, and local configuration would be super nice to have.

Mypy has a github action template, but it's more complicated than I'd expect:https://github.com/python/mypy/blob/master/action.yml

The last time I wrote a mypy CI action, it looked like this:

name: mypyon:  push:    branches:      - master  pull_request:    branches:      - masterjobs:  lint:    runs-on: ubuntu-latest    steps:    - uses: actions/checkout@v2    - uses: actions/setup-python@v2      with:        python-version: 3.8    - run: pip install -r requirements-dev.txt    - run: mypy <directory>
@Josverl
Comment options

I published a GH Action to allow typechecking of any MicroPython code by adding a stub package , and creating a pyproject.toml file in your repo. take your pick of pyright and/or mypy

https://github.com/marketplace/actions/micropython_type_checking

on:push:workflow_dispatch:jobs:CI:runs-on:ubuntu-latestname:MicroPython - CIsteps:      -uses:actions/checkout@v4      -name:Validate codeuses:josverl/mp_typecheck@latestwith:typechecker:pyright, mypymp-stubs:micropython-rp2-pico_w-stubs
@yangdanny97
Comment options

Awesome!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
General
Labels
None yet
2 participants
@yangdanny97@Josverl

[8]ページ先頭

©2009-2025 Movatter.jp