Mypy: Add ability to ignore a file or directory without modifying it

Created on 3 Mar 2018  路  13Comments  路  Source: python/mypy

In a project I maintain, we vendor some third party Python code and the project includes generated files such as files created by python-versioneer.

What I'd like to do is type check all files, including any new files, apart from those that I have explicitly ignored.

Right now I have the following hack workaround:

import subprocess
import sys


def main() -> None:
    args = ['mypy', '.']
    ignore_paths = {
        'src/dcos_e2e/_vendor',
        'src/dcos_e2e/_version.py',
        'versioneer.py',
    }
    result = subprocess.run(args=args, stdout=subprocess.PIPE)
    result_lines = result.stdout.decode().strip().split('\n')
    error_lines = [
        line for line in result_lines
        if not any(line.startswith(path) for path in ignore_paths)
    ]
    print('\n'.join(error_lines))
    sys.exit(int(bool(error_lines)))


if __name__ == '__main__':
    main()

Various tools have mechanisms for doing this, for example:

Adam@MacBook-Pro ~/D/m/d/dcos-e2e> cat .isort.cfg 
[settings]
skip=_vendor,
     src/dcos_e2e/__init__.py,
     versioneer.py,
     setup.py,
[flake8]
exclude=./src/dcos_e2e/_vendor/,
        ./src/dcos_e2e/_version.py,
        ./versioneer.py,
Adam@MacBook-Pro ~/D/m/d/dcos-e2e> cat pylintrc | grep ignore= -A 3
ignore=CVS,
       _vendor,
      versioneer.py,
      _version.py,

Thank you for the software.

Most helpful comment

If you're just going to ignore the errors from specific paths, a more elegant solution is to use module-specific sections in your mypy.ini with ignore_errors = True -- see config file docs.

All 13 comments

If you're just going to ignore the errors from specific paths, a more elegant solution is to use module-specific sections in your mypy.ini with ignore_errors = True -- see config file docs.

I think that will work and it is something that I was not aware of and I probably should have read the docs closer. Thank you, I will close this.

If you're just going to ignore the errors from specific paths, a more elegant solution is to use module-specific sections in your mypy.ini with ignore_errors = True -- see config file docs.

The proposed solution doesn't work in case the mypy.ini is being controlled by a different entity (DevOps for example).
There is a need for a local solution

If you're just going to ignore the errors from specific paths, a more elegant solution is to use module-specific sections in your mypy.ini with ignore_errors = True -- see config file docs.

I have a django codebase, where mypy is catching type errors in auto-generated migration files
These files live in different apps eg. customer/migrations/*.py and myapp/migrations/*.py. In such cases it might be good to add a glob or something to ignore such files instead of adding an individual entry for each app in the codebase(and also each new app we add).
Similarly we might want to ignore unit test case files across the project in different sub directories, but which all named say test_*.py

@danielbraun89

The proposed solution doesn't work in case the mypy.ini is being controlled by a different entity (DevOps for example).

Wait, so you control neither mypy.ini nor the source code in this scenario? (Read the initial description.) That seems a little out of the ordinary.

@sidmitra

In such cases it might be good to add a glob or something to ignore such files

Yes, [mypy-<package>] entries in mypy.ini support globs (but for module names, not filenames, so e.g. customer.migrations.* instead of customer/migrations/*.py).

I have a Qt resource file compiled with pyrcc, which I want to ignore. I can't add #type: ignore at the top of the file, which is regenerated by Qt.
My mypy.ini is common to all my python projects and is stored in a location which is not the root of the project.

I would like to add

[mypy-resources]
ignore_errors = True

or

[mypy-**resources]
ignore_errors = True

to ignore all resources.py files recursively... neither works.

@Djedouas I think that..

[mypy-*.resources]
ignore_errors = True

...should work for you. Does it?

@brandtbucher thanks, it works ! I was sure I had tested this pattern before, but apparently not... ;-)

I am having same issue with a _version.py file which is generated by versioneer.

Another tool supporting ignore, exclude or skip to add to @adamtheturtle's list is pre-commit. I use it to automatically run black on Python files, also makes sure files end with newline, json and yaml files are valid, etc.

A bit clunky in that their yaml doesn't support an excludes list, just a single regex.

# ...
exclude: "(.idea/)|(src/my_project/__init__.py)|(src/my_project/_version.py)|(versioneer.py)"

Pardon the comment on a closed issue, but this seems to be the most appropriate place.

In https://github.com/pypa/twine/pull/619, I'm ignoring the tests (primarily to avoid noise in my editor) by making tests a package and using:

[mypy-tests.*]
ignore_errors = True

However, there's some concern about the side-effects of making tests a package, and it does feel a little hacky to me. Is there another option?

More detail at https://github.com/pypa/twine/pull/619/files#r423015876.

I want to ignore errors in unit test files. My unit tests are co-located with the code they test, under <module-name>_test.py, and it doesn't seem to be possible to target those files with a mypy section. This doesn't work:

[mypy-*_test,*.*_test]
ignore_errors = True

@Hubro In order to ignore tests as you're attempting to do, I've resorted to making the tests/ directory a package, by adding a __init__.py. See https://github.com/python/mypy/issues/4675#issuecomment-627272252 for details.

Of course, that solution depends on your project structure. Also, if you're using setuptools to create a distribution, it could also result in your tests directory being included as a package (but you can work around that).

For Django migrations:

[mypy-*.migrations.*]
ignore_errors = True
Was this page helpful?
0 / 5 - 0 ratings