Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
14 tasks done
Josverl opened this issue Dec 13, 2023 · 7 comments · Fixed by #730
Open
14 tasks done

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

Josverl opened this issue Dec 13, 2023 · 7 comments · Fixed by #730
Assignees
Labels
documentation Improvements or additions to documentation Quality QA of published stubs

Comments

@Josverl
Copy link
Owner

Josverl commented Dec 13, 2023

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/

@Josverl Josverl added enhancement New feature or request stdlib micropython version of stdlib labels Dec 13, 2023
@Josverl Josverl assigned Josverl and unassigned Josverl Dec 13, 2023
Josverl added a commit that referenced this issue Dec 13, 2023
…sh pre-release

#729
Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
@gilesknap
Copy link

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 Josverl reopened this Dec 15, 2023
@Josverl
Copy link
Owner Author

Josverl commented Dec 15, 2023

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
Copy link

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
Copy link
Collaborator

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

@Josverl
Copy link
Owner Author

Josverl commented Dec 15, 2023

@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

@gilesknap
Copy link

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
Copy link
Owner Author

Josverl commented Dec 26, 2023

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 😎

@Josverl Josverl added documentation Improvements or additions to documentation and removed enhancement New feature or request stdlib micropython version of stdlib labels Feb 5, 2024
@Josverl Josverl added the Quality QA of published stubs label Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation Quality QA of published stubs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants