Josverl / micropython-stubs

Stubs of most MicroPython ports, boards and versions to make writing code that much simpler.
https://micropython-stubs.readthedocs.io
MIT License
133 stars 21 forks source link

micropython stdlib contains some modules that are not available on MicroPython #729

Open Josverl opened 6 months ago

Josverl commented 6 months ago

Raised on https://github.com/paulober/MicroPico/issues/170

micropython-stdlib contains (copies of) the typeshed stdlib

Some of these packages / modules are used internally by stdlib - so they cannot just be removed

Ill try to rename them in vscode ( which should also refactor the imports in the relevant modules that import these)

see : https://github.com/Josverl/micropython-stubs/blob/main/publish/micropython-stdlib-stubs/stdlib/

gilesknap commented 6 months ago

Hi @Josverl II'm trying to use the above to get the typing module.

I've done pip install micropython-stdlib-stubs --pre --target typings --no-user and I see a new 'typings' folder in my MicroPython project.

I've added an import from typings.stdlib.typing example code here: https://github.com/gilesknap/pico-xmas2/blob/74f0b45949fd3d51e3e83bf4818f6e5f2dbd4f79/asyn/button.py#L5-L26

I then use vscode and MicroPico extension to upload the project. When I try to run, I get the error;

>>> 
Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
ImportError: no module named 'typings'

Indeed it does look as though the typings folder is not uploaded to the pico.

Josverl commented 6 months ago

Ah, that is something different. The typing module is not available on MicroPython, only on your pc as part of the stubs.

There is a PR open to add that functionality, but there is no consensus yet.

Meanwhile you can typing add it to your source [in src/lib] .

I'll share a sample of a project where I have done that See: https://github.com/Josverl/MicroPicoDMX/tree/main/src%2Flib

Uploading those files on your board costs a few bytes of flash - but will avoid the ImportError and fiends

Also static types get optimized out during freezing or cross-compilation ( -O2 IIRC) so its not needed for production.

gilesknap commented 6 months ago

OK thanks, I'll try that over the weekend.

Now I'm not sure what your std-lib stubs added for my setup. Before I added your stubs vscode was allowing me to add typing info and I'm guessing that is because it's language server is reading my default system python libraries.

Does that make sense? Perhaps this question needs to go to @paulober as I'm not sure how his vscode extension makes use of the stubs?

paulober commented 6 months ago

The settings.json in your pico project redirects the Python language server for stubs into .vscode/Pico-W-Stub.

Josverl commented 6 months ago

@gilesknap

gilesknap commented 6 months ago

Hi @Josverl @paulober thanks for your help on this. I now have it working.

It seems the only thing I needed to do was to add the typing.py and typing_extensions.py stubs to the lib folder. That's it.

I did successfully install your stubs 1.0.1.a1 into .vscode/Pico-W-Stub but it does not make any difference as type checking in my vscode editor was already working anyway (not entirely sure why?).

But anyway the result is great - I get to use all the usual power of vscode and python static type checking for code that can run on the Pico.

Many thanks!

Josverl commented 6 months ago

Thanks for confirming.

add the typing.py and typing_extensions.py stubs to the lib folder.

Still thinking of a way to automate this, possibly using a mip install

not entirely sure why

The combined work of lots of people 😎