In this article we’ll explore how to use mypy, a static-type checker for python, with the SublimeText 3 code editor(henceforth SBT3). We’ll need to install the mypy python package and a plugin for SBT3 called Anaconda. Anaconda adds various features to SBT3 like: code completion, linting, code analyzer, etc.

The end result will be type checks for our python code while we type or when saving/loading a file. Let’s get started.

Installing Mypy

Mypy can be easily installed with pip. We can use the pip module from our current python interpreter to ensure the package gets installed for our current python version, like so:

1
python3.6 -m pip install mypy

Note: If the installation fails because of missing dependencies, you may need to install python-dev for the python version in use and then retry the instalation:

1
sudo apt-get install python3.6-dev

Note: If installation is failing because of lack of permission, try to install mypy using the user’s home directory with the --user flag:

1
python3.6 -m pip install mypy --user

Installing Anaconda

The Anaconda plugin can be installed with the Package Control for SBT3 by simply searching for anaconda.

Configuring Anaconda

Next up, we need to configure anaconda. We’ll add the following configs into anaconda’s user settings(Preferences -> Package Settings -> Anaconda -> Settings-User):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{

    "python_interpreter": "python3.6",

    "anaconda_linting_behaviour": "load-save",

    "anaconda_linter_phantoms": true,

    "mypy": true,
}

Here we:

  • set the python interpreter we want anaconda to use(this can be overridden on project basis);
  • make anaconda process python code only when the file is loaded or saved with anaconda_linting_behaviour and make anaconda display the errors inline with anaconda_linter_phantoms.
  • activate mypy;

All options along with their explanations can be found in Anaconda -> Settings-Default.

Using it

With everything setup properly we should be getting the type checks from mypy alongside other lint checks.

mypy code checking example