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 ?
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.
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.
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.
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 ./
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With