Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude folder from mypy checking

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 ?

like image 228
vianmixt Avatar asked Jun 09 '21 13:06

vianmixt


People also ask

How do I ignore MYPY errors?

You can use a special comment # type: ignore[code, ...] to only ignore errors with a specific error code (or codes) on a particular line. This can be used even if you have not configured mypy to show error codes. Currently it's only possible to disable arbitrary error codes on individual lines using this comment.

Where to put mypy config file?

To use this config file, place it at the root of your repo and run mypy. This config file specifies three global options in the [mypy] section.

What is Mypy_cache?

Incremental mode By default, mypy will store type information into a cache. Mypy will use this information to avoid unnecessary recomputation when it type checks your code again. This can help speed up the type checking process, especially when most parts of your program have not changed since the previous mypy run.


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
like image 199
Alexander Rakhmaev Avatar answered Nov 15 '22 10:11

Alexander Rakhmaev


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
like image 30
The Fool Avatar answered Nov 15 '22 11:11

The Fool


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
like image 34
mdryden Avatar answered Nov 15 '22 12:11

mdryden


This worked for me after some trial and error:

[mypy]
python_version = 3.9
disallow_untyped_defs = True
ignore_missing_imports = True
exclude = ['env/']
like image 22
mauricio777 Avatar answered Nov 15 '22 12:11

mauricio777


Ignoring more then one directories.

project.toml:

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

mypy --config-file pyproject.toml ./

like image 39
Novikov Avatar answered Nov 15 '22 12:11

Novikov