18

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?

0

4 Answers 4

24

You could use nbQA and do

pip install -U nbqa nbqa mypy your_notebook.ipynb 
Sign up to request clarification or add additional context in comments.

2 Comments

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!
Thanks @erb , much appreciated! Version 0.7.0 has just come out, it has a few fixes for mypy
9

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

Comments

4

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.