Reputation: 154545
I have a project containing a bunch of Python modules (.py
files) and a bunch of Jupyter Notebooks (.ipynb
files) which import things from the Python modules.
I can (assuming I've got __init__.py
files in all subfolders) type-check all the .py
files by simply running mypy .
from the root of the project. But I'd like to also be able to type-check my Jupyter Notebooks.
An ideal solution would:
.py
modules from within Jupyter Notebooks when type-checking, just like imports in .py
files,.py
files.How can I do this?
Upvotes: 18
Views: 13145
Reputation: 521
Checkout nb-mypy
Nb Mypy
is a facility to automatically run mypy on Jupyter notebook cells as they are executed, whilst retaining information about the execution history.
More details here
Upvotes: 7
Reputation: 10476
You could use nbQA and do
pip install -U nbqa
nbqa mypy your_notebook.ipynb
Upvotes: 24
Reputation: 7058
I use Jupytext and my IDE for this.
I export a copy in py:percent format, link that to the notebook. I Do the development in the jupyter lab environment, but the .py
file is the one that goes in the git repository. Before commiting, I run it throught the usual linters, black
, pydocstyle
, mypy
(with a strict configuration). I then reload the notebook in Jupyter lab, restart the kernel and 'Run All' to make sure the results are still OK, and only then commit the file to the repository
Upvotes: 3
Reputation: 1574
You can: Convert all notebooks to python, then run mypy on that (How do I convert a IPython Notebook into a Python file via commandline?).
jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb
Just write a small script to do this and you are fine :)
Upvotes: 4