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

stdlib: sys.print_exception is not known #770

Open
maxi07 opened this issue Dec 27, 2024 · 11 comments
Open

stdlib: sys.print_exception is not known #770

maxi07 opened this issue Dec 27, 2024 · 11 comments
Assignees
Labels
micropython-stdlib PyLance PyLance or PyRight related stdlib micropython version of stdlib

Comments

@maxi07
Copy link

maxi07 commented Dec 27, 2024

When calling

import sys
sys.print_exception(e)

I get the error message, that print_exception is not known - but it works fine when running on Micropython 1.23. So it seems like the stub is missing.

@Josverl
Copy link
Owner

Josverl commented Dec 27, 2024

Thanks for the report,

What port and board are you using, and which type checker is reporting the issue ?

If you are using mypy then it may be beyond its capabilities as it does not allow an alternative stdlib to be used

@maxi07
Copy link
Author

maxi07 commented Dec 27, 2024

I am using a raspberry pico w (1st gen) with vscode and pylance. I got the micropico extension for vscode.

@Josverl Josverl changed the title sys.print_exception is not known 1.23 stdlib: sys.print_exception is not known Dec 28, 2024
@Josverl Josverl self-assigned this Dec 28, 2024
@Josverl Josverl added PyLance PyLance or PyRight related stdlib micropython version of stdlib micropython-stdlib labels Dec 28, 2024
@jeff-phil
Copy link

Same issue with esp8266 board, and pyright and basepyright checkers. Thanks!

@Josverl
Copy link
Owner

Josverl commented Jan 19, 2025

sorry for the delay in responding,

Have you configured the correct path to typeshedPaths in vscode or pyproject.toml ?

https://micropython-stubs.readthedocs.io/en/main/22_vscode.html

@maxi07
Copy link
Author

maxi07 commented Jan 19, 2025

this is the content of my settings.json:

{
    "python.analysis.typeshedPaths": [
        "~/.micropico-stubs/included"
    ],
    "python.languageServer": "Pylance",
    "python.analysis.typeCheckingMode": "basic",
    "python.analysis.extraPaths": [
        "~/.micropico-stubs/included"
    ],
    "micropico.syncFolder": "",
    "micropico.openOnStart": true,
    "python.testing.pytestArgs": [
        "tests"
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.analysis.diagnosticSeverityOverrides": {
        "reportMissingModuleSource": "none"
    },
    "python.linting.enabled": true,
    "python.terminal.activateEnvironment": true
}

when I manually search for the file, it is located at ~/.micropico-stubs/included/sys.pyi - here I can actually see the print_exception function.

But if I actually use

import sys
sys.print_exception()

and right click the sys -> goto definition, I would end up in ~/.micropico-stubs/included/stdlib/sys/__init__.py, where the print_exception is missing.

Header of ~/.micropico-stubs/included/sys.pyi:

"""
System specific functions.

MicroPython module: https://docs.micropython.org/en/v1.23.0/library/sys.html

CPython module: :mod:`python:sys` https://docs.python.org/3/library/sys.html .

---
Module: 'sys' on micropython-v1.23.0-rp2-RPI_PICO_W
"""

# MCU: {'build': '', 'ver': '1.23.0', 'version': '1.23.0', 'port': 'rp2', 'board': 'RPI_PICO_W', 'mpy': 'v6.3', 'family': 'micropython', 'cpu': 'RP2040', 'arch': 'armv6m'}
# Stubber: v1.23.0

Header of ~/.micropico-stubs/included/stdlib/sys/__init__.py

"""
System specific functions.

MicroPython module: https://docs.micropython.org/en/v1.23.0/library/sys.html

CPython module: :mod:`python:sys` https://docs.python.org/3/library/sys.html .
"""

@maxi07
Copy link
Author

maxi07 commented Jan 19, 2025

I should have added the information that I am working with the paulober/micropico extension, that did the setup for me

@Josverl
Copy link
Owner

Josverl commented Jan 20, 2025

Yes, I recognized the micropico configuration

What happens if you
right click the sys -> goto type definition ?
For me it loads me to : typings\sys.pyi, relative to my project folder.

Also, in a plain manual configuration this works just fine.
Image

could you try changing the ~/.micropico-stubs/included to an absolute path, to see if perhaps the ~ is the culprit ?

@maxi07
Copy link
Author

maxi07 commented Jan 20, 2025

See my previous comment, while the sys.pyi file actually does exist, it would point me to ~/.micropico-stubs/included/stdlib/sys/__init__.py

@Josverl
Copy link
Owner

Josverl commented Jan 20, 2025

one one hand it is clear that it exists - on the other hand it can't be found.
Looking for possible cause :

  • one path uses a relative path structure that is supported by just about any OS on the planet
  • the other uses a ~ , that even on OO+Ses that support that , requires a different handling of paths

@paulober , I think you have experimented with some different settings for the typeshedPaths.
Was there any clear outcome ?

@paulober
Copy link
Collaborator

paulober commented Jan 23, 2025

I was able to repo the same issue.

I don't remember why we need the typeshedPaths setting which isn't event documented public in the first place. Sorry, it's been a while since I last worked on it...

I was able to solve the issue by adding following setting:

"python.analysis.stubPath": "~/.micropico-stubs/included"

Removing the typeshedPaths setting or changing it to ..../stdlib (for example) also solves this specific issue. So I would assume that the typeshedPaths overwrite the extraPaths and because typeshedPaths expects every module to have it's own sub directory stdlib/sys is favored over sys.pyi (which is only detect by extraPaths which seems to be lower ranked than the other setting).

@Josverl
Copy link
Owner

Josverl commented Jan 24, 2025

Thanks Paul,

The tools behaviour and settings change over time.
I do test for correct operations, but not using the extrapath settings.

Maybe I should add that to the test suite, if that is indeed a supported config by pyright.

I'm currently redesigning the stdlib stubs, so would worth checking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
micropython-stdlib PyLance PyLance or PyRight related stdlib micropython version of stdlib
Projects
None yet
Development

No branches or pull requests

4 participants