DEV Community

Emmanuel Gautier
Emmanuel Gautier

Posted on • Originally published at emmanuelgautier.com on

Enable Python type checking in VSCode

Since version 3.5, Python now has support for type hints. This typing is a cool new feature allowing type checking across your code for more quality and also help when you are using some packages or call some functions your colleague did in a large codebase. In this article, we will see how to enable type IntelliSense and type checking analysis in Visual Studio Code editor.

First of all, you need to install the Microsoft extension Pylance. This extension provides a set of useful features powered with Pyright, the Microsoft static type checking tool.

With the extension installed and enabled, you should now have better IntelliSense with typing information when you are calling some package function for example. For the type checking analysis, it is not enabled by default, you need to configure it by yourself.

In your settings.json file, add a new line with the following setting:

{
  "python.analysis.typeCheckingMode": "basic"
}
Enter fullscreen mode Exit fullscreen mode

The default value for this line is off meaning the static analysis is disabled. You have two other possible values which are:

  • basic: basic type checking rules
  • strict: All type checking rules at the highest error severity

If you test on the code below you should have a type error in VSCode now

# Wrong type between expected return type and the value type really returned by this function
def wrong_return_type() -> str:
    return False
Enter fullscreen mode Exit fullscreen mode

Discussion (0)