pythonvisual-studio-codepython-typingflake8

Python type hinting with Visual Studio Code & Flake8 plugin (without "missing" Annotations)


I have a bigger Python project and want to introduce type hinting step-by-step (I would like to suppress the "Missing type annotation" messages for now) to give me (coming from the strongly typed world) more confidence with Python. But I'm struggling to setup Flake8 with some meaningful type hinting in VS Code.

I expected some mechanism to do lint-time type checking when calling functions, for example, but it seems the Flake8 Annotations plugin doesn't seem to care at all. Do I have a misconception about what type hinting is all about?

Also type annotations (ANN###) won't show up in VS Code at all. How can I setup VS Code to squiggle annotated code as warning (yellow)?

Python VS Code extension is installed of course and I experimented with various python.linting.flake8Args in settings.json. I also installed the Python Type Hint extension, which does nice auto-completes but doesn't affect linting.

Anybody here who is successfully working on projects with type hinting in VS Code?


Solution

  • Ok, I was mislead by package descriptions: flake8-annotations is no replacement for mypy and does not perform type checks on its own, but is meant as a mere means to enforce type hints as pointed out here.

    Edit: I now ended up using pyright (included in VS Codes Pylance extension).

    Just wrote a pyrightconfig.json and put it in the workspace root folder:

    {
        "include": [
            "src/my_project/my_file_to_start_typehinting.py"
        ],
        "exclude": [
            "src/some_dependency"
        ],
        "ignore": [
            "src/some_dependency"
        ],
        "reportMissingImports": false,
        "reportMissingModuleSource": false,   
    }
    

    In the Pylance extension I switched the Analysis > Type Checking Mode to Basic and got my squiggles.

    I also found it useful to have the command-line tool installed:

    pip install pyright