Josverl/micropython-stubs

micropython stdlib contains some modules that are not available on MicroPython

Opened this issue · 7 comments

Raised on paulober/MicroPico#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)

  • codecs
  • contextlib
  • contextvars
  • dataclasses
  • decimal
  • enum
  • fractions
  • functools
  • numbers
  • queue
  • selectors
  • sre_compile
  • sre_constants
  • sre_parse

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

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.

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.

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?

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

@gilesknap

  • just delete the entire typings folder
  • then run pip install -U micropython-stdlib-stubs --pre --target .vscode/Pico-W-Stub --no-user
    to update the stdlib stubs in that location

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!

Thanks for confirming.

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

  • I'll add that to the documentatio
  • and sample repro.

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

not entirely sure why

The combined work of lots of people 😎