40

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.

6
  • 3
    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 9, 2021 at 14:26
  • 1
    It does not work. It generates the following error: mypy.ini: [mypy]: Unrecognized option: exclude = venv/
    – vianmixt
    Jun 9, 2021 at 15:07
  • What version of mypy are you using? The exclude option was added in 0.810.
    – mihi
    Jun 9, 2021 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 9, 2021 at 15:37
  • Huh, very strange
    – mihi
    Jun 9, 2021 at 16:41

7 Answers 7

33

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
5
  • 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. Dec 3, 2021 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, 2022 at 13:15
  • What settings.json file? Where?
    – cowlinator
    Apr 11, 2023 at 2: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, 2023 at 10:28
  • 2
    python.linting.ignorePatterns is now deprecated. It can be replaced with python.analysis.ignore which I guess will apply to any configured linter, not just mypy
    – benelgiac
    Oct 4, 2023 at 8:24
21

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

[mypy]
exclude = venv
2
  • In VSCode depending on the extension in use this my be by passed. See the next answer in that case. Dec 3, 2021 at 18:18
  • 1
    Works in setup.cfg too. Sep 30, 2022 at 16:42
18

Ignoring more than one directory.

pyproject.toml:

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

mypy --config-file pyproject.toml ./

2
  • 1
    do you mean project.toml or pyproject.toml?
    – cowlinator
    Apr 11, 2023 at 2:55
  • 2
    It automatically reads pyproject.toml and you can skip specifying it manually. Sep 12, 2023 at 12:01
9

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

2
  • 1
    This should be the top answer. I was always wondering, why the other approaches don't work for me. Thanks Mar 13, 2023 at 10:53
  • 2
    Does not appear to work.
    – Harlin
    Jun 20, 2023 at 20:22
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
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/']
2
  • 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, 2021 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. Sep 16, 2021 at 19:53
0

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"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.