'MyPy configuration - exclude multiple directories

We're currently using Mypy (v 0.910) in our project with pyproject.toml for configuration.

I have the following file structure:

src
  --app
    --generated
    --service
    --data
  --ingest

pyproject.toml:

...
[tool.mypy]
python_version = 3.8
disallow_untyped_defs = true
exclude = "(src/app/generated)|(src/ingest)"
...

When running with this configuration, the src/ingest folder is ignored, but not the src/app/generated folder. To test the regex, I also tried:

...
[tool.mypy]
python_version = 3.8
disallow_untyped_defs = true
exclude = "(src/app)|(src/ingest)"
...
mypy src --config-file ./pyproject.toml 
Success: no issues found in 1 source file

which successfully ignored all files. I'm wondering why the first example is not ignoring src/app/generated folder.



Solution 1:[1]

Following should work:

[tool.mypy]
python_version = 3.8
disallow_untyped_defs = true
exclude = "src/(app|ingest)"

Solution 2:[2]

for whoever uses setup.cfg file. The following syntax is the one that worked for me:

[mypy]
exclude = folder_1|venv|tests

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ehud Lev