microsoft / vscode-mypy

Linting support for Python using the mypy linter.
https://marketplace.visualstudio.com/items?itemName=ms-python.mypy-type-checker
MIT License
114 stars 24 forks source link

Mypy extension for Visual Studio Code

A Visual Studio Code extension with support for the Mypy type checker. This extension ships with mypy=1.11.2.

Note: The minimum version of Mypy this extension supports is 1.0.0. If you have any issues with Mypy, please report it to this issue tracker as this extension is just a wrapper around Mypy.

This extension supports all actively supported versions of the Python language (i.e., Python >= 3.8).

For more information on Mypy, see https://www.mypy-lang.org/

Usage and Features

The Mypy extension provides a series of features to help your productivity while working with Python code in Visual Studio Code. Check out the Settings section for more details on how to customize the extension.

Disabling Mypy

You can skip type checking with Mypy for specific files or directories by setting the mypy-type-checker.ignorePatterns setting.

If you wish to disable Mypy for your entire workspace or globally, you can disable this extension in Visual Studio Code.

Settings

There are several settings you can configure to customize the behavior of this extension.

Settings Default Description
mypy-type-checker.args [] Arguments passed to Mypy to enable type checking on Python files. Each argument should be provided as a separate string in the array.
Example:
"mypy-type-checker.args" = ["--config-file=<file>"]
mypy-type-checker.cwd ${workspaceFolder} Sets the current working directory used to lint Python files with Mypy. By default, it uses the root directory of the workspace ${workspaceFolder}. You can set it to ${fileDirname} to use the parent folder of the file being linted as the working directory for Mypy. You can also set it to ${nearestConfig} to use the nearest parent/ancestor folder which contains a pyproject.toml or a mypy.ini file.
mypy-type-checker.severity { "error": "Error", "note": "Information" } Mapping of Mypy's message types to VS Code's diagnostic severity levels as displayed in the Problems window. You can also use it to override specific Mypy error codes.
For example: { "error": "Error", "note": "Information", "name-defined": "Warning" }
mypy-type-checker.path [] Path or command to be used by the extension to type check Python files with Mypy. Accepts an array of a single or multiple strings. If passing a command, each argument should be provided as a separate string in the array. If set to ["mypy"], it will use the version of Mypy available in the PATH environment variable. Note: Using this option may slowdown type checking.
Examples:
- ["~/global_env/mypy"]
- ["conda", "run", "-n", "lint_env", "python", "-m", "mypy"]
mypy-type-checker.interpreter [] Path to a Python executable or a command that will be used to launch the Mypy server and any subprocess. Accepts an array of a single or multiple strings. When set to [], the extension will use the path to the selected Python interpreter. If passing a command, each argument should be provided as a separate string in the array.
mypy-type-checker.importStrategy useBundled Defines which Mypy binary to be used to type check Python files. When set to useBundled, the extension will use the Mypy binary that is shipped with the extension. When set to fromEnvironment, the extension will attempt to use the Mypy binary and all dependencies that are available in the currently selected environment. Note: If the extension can't find a valid Mypy binary in the selected environment, it will fallback to using the Mypy binary that is shipped with the extension. Note: The mypy-type-checker.path setting takes precedence and overrides the behavior of mypy-type-checker.importStrategy.
mypy-type-checker.showNotifications off Controls when notifications are shown by this extension. Accepted values are onError, onWarning, always and off.
mypy-type-checker.reportingScope file (experimental) Controls the scope of Mypy's problem reporting. If set to file, Mypy will limit its problem reporting to the files currently open in the editor. If set to workspace, Mypy will extend its problem reporting to include all files within the workspace.
mypy-type-checker.preferDaemon true (experimental) Whether the Mypy daemon (dmypy) will take precedence over mypy for type checking. Note: if mypy-type-checker.reportingScope is set to workspace, enabling the Mypy daemon will offer a faster type checking experience. This setting will be overridden if mypy-type-checker.path is set.
mypy-type-checker.ignorePatterns [] Configure glob patterns as supported by the fnmatch Python library to exclude files or folders from being type checked by Mypy.

The following variables are supported for substitution in the mypy-type-checker.args, mypy-type-checker.cwd, mypy-type-checker.path, mypy-type-checker.interpreter and mypy-type-checker.ignorePatterns settings:

The mypy-type-checker.path setting also supports the ${interpreter} variable as one of the entries of the array. This variable is subtituted based on the value of the mypy-type-checker.interpreter setting.

Commands

Command Description
Mypy: Restart Server Force re-start the linter server.

Logging

From the Command Palette (View > Command Palette ...), run the Developer: Set Log Level... command. Select Mypy Type Checker from the Extension logs group. Then select the log level you want to set.

Alternatively, you can set the mypy-type-checker.trace.server setting to verbose to get more detailed logs from the Mypy server. This can be helpful when filing bug reports.

To open the logs, click on the language status icon ({}) on the bottom right of the Status bar, next to the Python language mode. Locate the Mypy Type Checker entry and select Open logs.

Troubleshooting

In this section, you will find some common issues you might encounter and how to resolve them. If you are experiencing any issues that are not covered here, please file an issue.