pythonvisual-studio-codepython-typing

type hints (warnings) for Python in VS Code


I enjoy using type hinting (annotation) and have used it for some time to help write clean code.

I appreciate that they are just hints and as such do not affect the code.

But today I saw a video where the linter picked up the hint with a warning (a squiggly yellow underline), which looks really helpful. My VS Code does not pick this up in the linter.

Here is an image of what I expect (with annotations):

enter image description here

So my question is, how can I achieve this?

For example, is there a specific linter or setting that would do this?


Solution

  • If you are using Pylance, you can add a new line to your settings.json (you need to restart VS Code after updating the file):

    "python.analysis.typeCheckingMode": "basic"
    

    The default value is off, the other possible values are basic and strict.

    The following screenshot shows warnings for different situations: wrong variable type, wrong function return type and wrong parameter type.

    enter image description here