Python type checking in VS Code

29,550

Solution 1

from bash

mkdir test
cd test
python3 -m venv .env
source .env/bin/activate
python -m pip install flake8
python -m pip install flake8-mypy
code ./

install plugin

then install this in VSCode https://marketplace.visualstudio.com/items?itemName=donjayamanne.python
and config

settings

./.vscode/settings.json

{
    "python.envFile": "${workspaceRoot}/.env",
    "python.pythonPath": ".env/bin/python",
    "python.linting.flake8Enabled": true,
    "python.linting.pylintEnabled": false,
    "python.linting.mypyEnabled": true,
}

./.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${file}",
            "cwd": "${workspaceRoot}",
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        }
    ]
}

OMG, this is Python 3 only!

https://pypi.python.org/pypi/flake8-mypy/17.3.3

Yes, so is mypy. Relax, you can run Flake8 with all popular plugins
as a tool perfectly fine under Python 3.5+ even if you want to analyze
Python 2 code. This way you’ll be able to parse all of the new syntax
supported on Python 3 but also effectively all the Python 2 syntax at
the same time.

By making the code exclusively Python 3.5+, I’m able to focus on the
quality of the checks and re-use all the nice features of the new
releases (check out pathlib) instead of wasting cycles on Unicode
compatibility, etc.

IDE & Linter Integrations

https://github.com/python/mypy#ide--linter-integrations

IDE & Linter Integrations

Mypy can be integrated into popular IDEs:

  • Vim: vim-mypy
  • Emacs: using Flycheck and Flycheck-mypy
  • Sublime Text: SublimeLinter-contrib-mypy
  • Atom: linter-mypy
  • PyCharm: PyCharm integrates its own implementation of PEP 484.

Mypy can also be integrated into Flake8 using flake8-mypy.

o ,-

Solution 2

I added the following code

{
    "name": "mypy",
    "type": "python",
    "request": "launch",
    "module": "mypy",
    "args": [
        "${file}"
    ],
    "console": "integratedTerminal"
}

in VS Code launch.json and now it is visible in "DEBUG" window. Just press F5 and you you get full static analysis of the current file.

Share:
29,550
Author by

Alex Undefined

Updated on March 08, 2020

Comments

  • Alex Undefined almost 3 years

    I've recently learned about typing module in Python (https://docs.python.org/3/library/typing.html) and expected to use it for static type checking and for better intellisense in VS Code, like it works with TypeScript, but I can't seem to find any tools/plugins that actually do that.

    What are my options, if any?