Setting up Sublime Text 3 for Python Type Checking

Erika Dike
2 min readJun 30, 2017

--

So you have switched to python 3.6 and are eager to enjoy the benefits of python’s new static type checking. However, you enter code like the below on sublime text:

def get_first_name(full_name: str) -> str:

Only to have your code linter (in my case, Anaconda) flag it as invalid syntax. The second bummer is having to run mypy on the command line to detect static type checking violations. Now, that’s no fun if you are like me that prefers to get syntax violation feedback whenever I save the file I am working on.

Don’t despair, Sublime Text has plugins and settings that take care of these two scenarios.

Fix flagging of static type checking as invalid syntax (Anaconda)

This could most likely be because Anaconda’s python_interpreter setting is pointing to python 2 rather than ≥ python 3.5. I’d recommend you fix this by creating a sublime text project for your current project. Go to the .sublime-project file and add the following:

{
...
"settings":
{
"python_interpreter": "/path/to/python3"
}
}

Set up sublime text 3 to flag static type checking violations

The standard way you would see all over the internet to check for static type checking is to run “mypy”. This is great and should be not be totally done away with as it is the most stable way for running static checks currently. However, for instant feedback on your editor, simply install SublimeLinter-contrib-mypy through the sublime package manager. You could follow the instructions on their GitHub page to get up and running. This assumes that you have gotten Sublime linter to lint python code in the past.

Something that might trip you is not having PEP8 and mypy installed under python 3’s pip. Do install these packages with

$ python -m pip install pep8
$ python -m pip install mypy-lang

Alternatively, you can

$ pip3 install pep8
$ pip3 install mypy-lang

And thats all it takes to enjoy python’s static code analysis on Sublime Text editor.

--

--

Erika Dike

I write software and occasionally publish stuff about some things I found interesting.