Type checking for Python code#3382
Conversation
95db772 to
44ec379
Compare
83eb785 to
f585773
Compare
| matplotlib | ||
| pandas>=2.1 | ||
| pandas-stubs | ||
| pyqir>=0.12.5,<0.13 |
There was a problem hiding this comment.
Why do these need to be installed for the checks? Does it need to verify the types from any package your our code may use? (In which case, would it need to ultimately extend to qiskit, cirq, etc.)?
There was a problem hiding this comment.
When typechecker sees import statement and library is not installed, it raises an error (both mypy and pyright).
There are 2 ways to deal with it:
- Have the library installed (or "type stubs" which provides type signatures for public API) - then you get all calls to the library type-checked.
- Ignore this import - then all calls to the library are not type-checked, reducing usefulness of type checking.
We can make this choice for every library we import, this is why I created this separate requirements file. I chose to install matplotlib, pandas and pyqir rather than ignoring them.
Yes, I intend to ultimately extend it here to use other libraries, like cirq and qiskit, but I want to be able to make this choice for each library. If I see that some library would take a lot of time to install (e.g. cirq that has a lot of dependencies) and is used only in few limited places, I can ignore it or maybe only install type stubs.
| ) | ||
|
|
||
| parser.add_argument( | ||
| "--check-py", |
There was a problem hiding this comment.
The existing check flag does JavaScript/TypeScript and Rust. Is there a reason to add a distinct Python check flag than just roll it into the existing check (if building a Python package).
There was a problem hiding this comment.
Yes,
- So I can run Python checks only without having to wait for Rust and TypeScript checks that might be irrelevant for me if I am only working on Python code.
- So we can call call
build.py --check-pyin CI workflow for python static checks.
There was a problem hiding this comment.
If we're going to split it out, I'd lean towards splitting all of them out and then having check mean "check all".
There was a problem hiding this comment.
I talked to @swernli about this and he suggested the same - having an option in build.py to run every check separately, but this can be done in later PR.
This option exists for the following scenario: let's say you did some change to Python and "static Python check" workflow failed. You want to do fixes, verify them locally, push your changes and get workflow green as soon as possible. You do your local changes, run build.py --check-py, it passes, then you push your changes and the workflow is green.
Without this option, there would be no way to run python static checks only.
| "source/qdk_package/qdk/cirq", | ||
| "source/qdk_package/qdk/simulation", | ||
| "source/qdk_package/qdk/qiskit", | ||
| "source/qdk_package/qdk/qre" |
There was a problem hiding this comment.
Do we plan to include this at a later point?
There was a problem hiding this comment.
Yes, in follow-up PRs.
This PR:
build.py. Currently it only runs type checks using pyright, but more static checks (line length, linter, formatter) can be added here.Notes:
source/qdk_package/qdk.check_requirements.txtthat includes minimal dependencies that are required to run static checks. This minimizes setup time for CI workflow.The type checking can be done in multiple ways:
pyright- runs pyright directly../build.py --check-py- recommended. This installs required dependencies and runs mypy, and doesn't do anything else. If this passes locally, it's guaranteed that it will pass on CI.python ./build.py --check-py --no-check-prereqs- like above, but skips checking prerequisites (like wasm) which are irrelevant for Python. This is used in the CI workflow.build.pyin a way that includes building qdk and doesn't disable checks. This includesbuild.py --qdkandbuild.pywith no arguments.Missing checks (will be added in follow-up PRs):
"reportMissingParameterType": "error"and"typeCheckingMode": "strict"in pyrightconfig.json, we get 2745 errors. I plan to gradually fix them in a series of PRs.