Instantly share code, notes, and snippets.

What would you like to do?
A cell magic to enable the use of mypy within jupyter notebooks
Add mypy type-checking cell magic to jupyter/ipython.
Save this script to your ipython profile's startup directory.
IPython's directories can be found via `ipython locate [profile]` to find the current ipython directory and ipython profile directory, respectively.
For example, this file could exist on a path like this on mac:
where /Users/yourusername/.ipython/profile_default/ is the ipython directory for
the default profile.
The line magic is called "typecheck" to avoid namespace conflict with the mypy
from IPython.core.magic import register_cell_magic
def typecheck(line, cell):
Run the following cell though mypy.
Any parameters that would normally be passed to the mypy cli
can be passed on the first line, with the exception of the
-c flag we use to pass the code from the cell we want to execute
%%typecheck --ignore-missing-imports
mypy stdout and stderr will print prior to output of cell. If there are no conflicts,
nothing will be printed by mypy.
from IPython import get_ipython
from mypy import api
# inserting a newline at the beginning of the cell
# ensures mypy's output matches the the line
# numbers in jupyter
cell = '\n' + cell
mypy_result =['-c', cell] + line.split())
if mypy_result[0]: # print mypy stdout
if mypy_result[1]: # print mypy stderr
shell = get_ipython()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment