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

feat(sage-monorepo): type check Python projects with Pyright CLI and VS Code extension #2840

Merged

Conversation

tschaffter
Copy link
Member

@tschaffter tschaffter commented Sep 21, 2024

Description

integrate Pyright for type checking in Python projects within this monorepo

Motivation for integrating Pyright:

  • Pyright is often 3x to 5x faster than mypy, according to its authors.
    • It uses "just-in-time" type checking rather than analyzing all code in a module from top to bottom.
    • Independent benchmark: TBD.
  • Pyright was designed to be used as the foundation for a Python language server, providing better integration with VS Code.

Changelog

  • Add Pyright VS Code extension to the dev container
  • Add Pyright CLI (npm package)
  • Add the task sandbox-py-app:type-check
  • Type check Python projects affected by staged files

References

Preview

Python file to type check with two voluntary mistakes:

import sandbox_py_lib.nonexistent_module  # This module does not exist

"""Sample Hello World application."""


def hello_lib():
    return sandbox_py_lib.hello.nonexistent_function()  # This function does not exist

Type checking:

$ nx type-check sandbox-py-app

> nx run sandbox-py-app:type-check

> poetry run pyright

/workspaces/sage-monorepo/apps/sandbox/py-app/sandbox_py_app/hello.py
  /workspaces/sage-monorepo/apps/sandbox/py-app/sandbox_py_app/hello.py:1:8 - error: Import "sandbox_py_lib.nonexistent_module" could not be resolved (reportMissingImports)
  /workspaces/sage-monorepo/apps/sandbox/py-app/sandbox_py_app/hello.py:7:27 - error: "hello" is not a known attribute of module "sandbox_py_lib.nonexistent_module" (reportAttributeAccessIssue)
2 errors, 0 warnings, 0 informations 
Warning: command "poetry run pyright" exited with non-zero status code
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Ran target type-check for project sandbox-py-app (2s)

   ✖  1/1 failed
   ✔  0/1 succeeded [0 read from cache]

Notes

Pyright can be installed a Python or npm package. It looks like the Python package runs JS code (see dependency on nodeenv below).

$ poetry remove pyright
Updating dependencies
Resolving dependencies... (0.2s)

Package operations: 0 installs, 0 updates, 2 removals

  - Removing nodeenv (1.9.1)
  - Removing pyright (1.1.381)

Writing lock file

@tschaffter tschaffter marked this pull request as ready for review September 21, 2024 05:15
@tschaffter tschaffter changed the title feat(sage-monorepo): explore type checking with Pyright CLI and VS Code extension feat(sage-monorepo): type check Python projects with Pyright CLI and VS Code extension Sep 21, 2024
Copy link

Copy link

Copy link

Copy link

Quality Gate Passed Quality Gate passed for 'openchallenges-challenge-service'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link

Quality Gate Passed Quality Gate passed for 'openchallenges-organization-service'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link

Quality Gate Passed Quality Gate passed for 'openchallenges-image-service'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

@tschaffter tschaffter merged commit 16cf257 into Sage-Bionetworks:main Sep 22, 2024
15 checks passed
@tschaffter tschaffter deleted the sage-monorepo/explore-pyright branch September 22, 2024 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant