Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type-check Jupyter Notebooks with mypy

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?

like image 231
Mark Amery Avatar asked Nov 21 '19 13:11

Mark Amery


1 Answers

You could use nbQA and do

pip install -U nbqa
nbqa mypy your_notebook.ipynb
like image 172
ignoring_gravity Avatar answered Oct 05 '22 04:10

ignoring_gravity