37

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 ?

vianmixt
  • 703
  • 1
  • 6
  • 17
  • 2
    You'll probably need to remove the quotation marks: `exclude = /venv/`, and maybe the initial `/` as well: `exclude = venv/` (depending on whether `venv` is a sibling of your ini file or located somewhere deeper) – mihi Jun 09 '21 at 14:26
  • 1
    It does not work. It generates the following error: mypy.ini: [mypy]: Unrecognized option: exclude = venv/ – vianmixt Jun 09 '21 at 15:07
  • What version of mypy are you using? The exclude option was added in 0.810. – mihi Jun 09 '21 at 15:33
  • mypy 0.901 and mypy-extensions 0.4.3 Also I was using mypy vs-code extension 0.2.0 that was released last month. It looks like reverting back to 0.1.5 fixed the issue. Not sure yet. – vianmixt Jun 09 '21 at 15:37
  • Huh, very strange – mihi Jun 09 '21 at 16:41
  • you missed the `=` and mypy is pretty unhelpful. – dcsan Sep 13 '21 at 17:17

6 Answers6

29

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
The Fool
  • 16,715
  • 5
  • 52
  • 86
  • Depends on the extension in use Microsoft's version of this check my very well be per file. There is also an independent mypy extension that uses the workspace folder as its default for starting mypyd. – wheredidthatnamecomefrom Dec 03 '21 at 18:16
  • What would the glob pattern for `python.linting.ignorePatterns` look like when ignoring a directory and all of its subdirectories? – mallwright Sep 14 '22 at 13:15
  • What `settings.json` file? Where? – cowlinator Apr 11 '23 at 02:56
  • @cowlinator , the settings.json holds all your vscode configurations. You can open it with `ctrl+shift+p` and then typing *settings* in the search bar. – The Fool Apr 11 '23 at 10:28
21

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
11

Ignoring more then one directories.

pyproject.toml:

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

mypy --config-file pyproject.toml ./

eNca
  • 1,043
  • 11
  • 21
Novikov
  • 504
  • 6
  • 12
8

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).

eteng
  • 107
  • 1
  • 4
7

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
mdryden
  • 965
  • 1
  • 9
  • 12
5

This worked for me after some trial and error:

[mypy]
python_version = 3.9
disallow_untyped_defs = True
ignore_missing_imports = True
exclude = ['env/']
mauricio777
  • 1,296
  • 1
  • 15
  • 15
  • 1
    that excluded my whole project lol. the only thing that seems to work is just plain `exclude = venv/` with no quotes. – dcsan Sep 13 '21 at 17:16
  • 1
    I made the venv dir named 'env'. This was the only thing that worked for me. I'm on linux, perhaps that might influence the results. – mauricio777 Sep 16 '21 at 19:53