15

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:

  • type check all Python code in my Jupyter Notebooks,
  • be able to follow imports of .py modules from within Jupyter Notebooks when type-checking, just like imports in .py files,
  • let me type-check the whole project from the command line, so that I can run type-checking as part of a test suite or a pre-commit hook, and
  • in some way meaningfully report the locations of type errors within my Notebooks, analogously to how mypy prints line numbers for errors in .py files.

How can I do this?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
  • 1
    Did you ever find a solution? – Chris_Rands Jan 29 '20 at 14:23
  • 2
    @Chris_Rands E. Serra's answer below is probably the seed that any solution will be based on, but building a really satisfactory script from it is tricky. I made a start but wasn't satisfied; I may self-answer some day if nobody beats me to it. – Mark Amery Jan 29 '20 at 16:27

4 Answers4

20

You could use nbQA and do

pip install -U nbqa
nbqa mypy your_notebook.ipynb
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
  • 2
    This answer is great as it provides a general solution to the problem of running linters/formatters/etc on notebooks. Thanks for writing it @ignoring_gravity! – erb Apr 18 '21 at 15:14
  • 2
    Thanks @erb , much appreciated! Version 0.7.0 has just come out, it has a few fixes for mypy – ignoring_gravity Apr 18 '21 at 18:46
5

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 :)

E.Serra
  • 1,495
  • 11
  • 14
5

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

Sharukh Rahman
  • 327
  • 3
  • 12
3

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

Maarten Fabré
  • 6,938
  • 1
  • 17
  • 36