Exclude folder from mypy checking

Viewed 16209

I would like to exclude a folder from the mypy checks. Looking at the documentation I tried the following configuration in my mypy.ini configuration file

[mypy]  
python_version = 3.8  
exclude '/venv/'

with no luck.

Yes I want to exclude my virtual environment from mypy checking. I only one to type check the code that I write.

Is it a bug from mypy ?

5 Answers

dear friend. The answer was given in the comments. But I'll post a complete answer here so other people can find it easily.

If I want to ignore my venv folder, then I write the following lines to the mypy.ini file:

[mypy]
exclude = venv

The issue is that VS Code is invoking mypy file by file. And mypy doesn't use the exclude option when invoked on a single file. The workaround is using python.linting.ignorePatterns in the settings.json.

 "python.linting.ignorePatterns": [ "venv/**/*.py" ]

More info about the behaviour of exclude:

Note that this flag only affects recursive discovery, that is, when mypy is discovering files within a directory tree or submodules of a package to check. If you pass a file or module explicitly, it will still be checked. For instance, mypy --exclude '/setup.py$' but_still_check/setup.py.

I would still exclude this in the config file, for completeness’ sake, in case you run mypy by hand.

[mypy]
exclude = venv

In my case, the issue was the vscode linting in my tests folder. The solution listed above ( "python.linting.ignorePatterns": [ "test" ]) likely would have worked, but I didn't want to disable linting there completely. What worked for me was adding this to my mypy.ini:

[mypy]
# global config here

[mypy-test.*] # the folder I wanted ignored was named "test" and is in the root of my workspace.
ignore_errors = True

This worked for me after some trial and error:

[mypy]
python_version = 3.9
disallow_untyped_defs = True
ignore_missing_imports = True
exclude = ['env/']

Ignoring more then one directories.

project.toml:

[tool.mypy]
exclude = ['venv', '.venv']

mypy --config-file pyproject.toml ./

Related