vianmixt
vianmixt

Reputation: 883

Mypy does not respect setting in mypy.ini to exclude folder from checking when called from VS Code

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.

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?

I am using mypy 0.901 and mypy-extensions 0.4.3. Also I am using mypy vs-code extension 0.2.0.

Upvotes: 45

Views: 71307

Answers (7)

Archirk
Archirk

Reputation: 641

At the moment of my answer, Microsoft's Mypy extensions has experimental(as the write) setting: mypy-type-checker.reportingScope.
Setting this option to workspace and restarting mypy server helped me to disable mypy checks in my tests directory.

// settings.json
"mypy-type-checker.reportingScope": "workspace"
# pyproject.toml
[tool.mypy]
exclude =  ["tests"]

Upvotes: 1

Novikov
Novikov

Reputation: 730

Ignoring more than one directory.

pyproject.toml:

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

mypy --config-file pyproject.toml ./

Upvotes: 20

Alexander Rakhmaev
Alexander Rakhmaev

Reputation: 1055

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

[mypy]
exclude = venv

Upvotes: 21

eteng
eteng

Reputation: 137

According to the official doc, if using mypy.ini or setup.cfg, the content of the exclude field in the configuration file seems have to be a regular expression, e.g.:

[mypy]
exclude = (?x)(
    ^one\.py$    # files named "one.py"
    | two\.pyi$  # or files ending with "two.pyi"
    | ^three\.   # or files starting with "three."
  )

On the other hand, if you use pyproject.toml as the configuration file, the content of the exclude field could be of two forms: (1) a single regular expression (as above); (2) an array of strings as follows:

[tool.mypy]
exclude = [
    "^one\\.py$",  # TOML's double-quoted strings require escaping backslashes
    'two\.pyi$',  # but TOML's single-quoted strings do not
    '^three\.',
]

So, it seems that using an array of file/dir-names (not a RegEx) be not allowed for MyPy, which is different from that of flake8 (just only an array of file/dir-names is OK).

Upvotes: 10

mdryden
mdryden

Reputation: 1005

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

Upvotes: 8

The Fool
The Fool

Reputation: 20537

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

Upvotes: 35

mauricio777
mauricio777

Reputation: 1436

This worked for me after some trial and error:

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

Upvotes: 5

Related Questions