This is a fork of Matan Gover's mypy extension with a few extras, including:
- Support for environment variables (
mypy.envFile
,mypy.env
), originally from this PR: matangover#101 - Support for root definition (
mypy.roots
), originally from this PR: matangover#100 - And a few fixes for both.
Original README below.
Runs mypy on Python code to provide type checking.
-
Runs on your entire workspace. (This is different from Microsoft's Python extension's mypy functionality which only lints each file separately, leading to incomplete type checking.)
-
Uses the mypy daemon to keep the analysis state in memory so that only changed files are rechecked.
-
Respects the active Python interpreter (set in the Python extension) and the
mypy.ini
configuration file. -
Supports multi-root workspaces: will launch a separate mypy daemon for each workspace folder.
This extension requires mypy to be installed on your system. To install mypy, run pip install mypy
. There are other ways to install mypy, such as using pipx
or your system's package manager.
By default, this extension relies on having the dmypy
executable available on your PATH. This should be the case
if you installed mypy globally. To use a different mypy installation, set the mypy.dmypyExecutable
setting.
Some people prefer to have mypy installed in each project's virtual environment rather than in a global location. To do this, enable mypy.runUsingActiveInterpreter
(either globally or for a specific workspace).
The extension automatically checks the workspace with Mypy if there are Python files in it. Diagnostics generated by Mypy are shown in the editor and in the Problems panel.
Every time you save a Python file, the extension will re-run Mypy. Only the changed files (and their dependents) will be re-analyzed.
The extension also provides the following commands:
Mypy: Recheck Workspace
: Re-run mypy on the workspace. This is not normally necessary, but useful if you change Python files outside of VS Code or after you checkout a different Git branch.Mypy: Restart Daemon and Recheck Workspace
: Restart the mypy daemon and recheck. This is not normally necessary, but useful if mypy is behaving unexpectedly.
To configure mypy, you can create a mypy.ini
file in your workspace folder (or any of the default locations). See mypy configuration file. You can also specify a custom path to mypy.ini
using the mypy.configFile
setting.
To configure the mypy-vscode extension, use the following VS Code settings:
-
mypy.targets
: specify a list of target files or folders for mypy to analyze. By default the entire workspace folder is checked. You may prefer to use thefiles
option inmypy.ini
to specify which files mypy should analyze. In that case, you should setmypy.targets
to an empty array ([]
). -
mypy.roots
: Specify a list of root folders. Each root is checked by its own mypy daemon. If not set, the workspace folders are used. This is useful for monorepos when you are not using a normal multi-root workspace. This setting is ignored in multi-root workspaces. -
mypy.dmypyExecutable
: Path todmypy
(the mypy daemon). Either a full path or just a name (which must exist in your PATH). You can use substitutions:${workspaceFolder}
and~
(home directory). -
mypy.runUsingActiveInterpreter
: Use the active Python interpreter (selected in the Python extension) to run dmypy itself, instead of themypy.dmypyExecutable
setting. Note: your code is always checked against the active interpreter – this setting only controls the interpreter used to run dmypy itself. -
mypy.configFile
: Mypy config file, relative to the workspace folder. If empty, search in the default locations. See https://mypy.readthedocs.io/en/latest/config_file.html. -
mypy.extraArguments
: A list of extra command-line arguments to append to thedmypy run
command. For a list of options, see mypy's documentation. -
mypy.enabled
: Enable or disable Mypy checking. For example, you can disable Mypy for a specific workspace or folder. -
mypy.debugLogging
: Enable debug logging for the extension. (Reload the window after changing this setting.)
This extension can also run mypy on Python code cells in Jupyter notebooks. To enable this feature, set mypy.checkNotebooks
to true
. Notebooks are type checked when they're opened or saved.
For notebooks, we use mypy rather than dmypy. Use the mypy.mypyExecutable
setting to control the mypy executable path. Most settings like mypy.runUsingActiveInterpreter
, mypy.configFile
, mypy.extraArguments
are honored for notebooks too.
There are a couple of known issues:
- Notebook checking works by concatenating all code cells into a single document. This means that some errors may be reported on the wrong line, or that invalid code could appear valid or vice versa. It also means that if one cell has a syntax error, the rest of the notebook won't be checked.
- The extension can't access the Python interpreter selected for a notebook, so it uses the interpreter selected in the workspace instead. This may cause wrong type checking, e.g. a package will appear to be missing even though it is installed in the notebook's environment.
This project is made available under the MIT License.