Search code examples
pythonlintflake8

Flake8 - how to exclude all files from a project by listing every file?


I am setting up flake8 on a legacy Python code base. My idea was to explicitly list the names of all the .py files present in a form similar to this:

[flake8]
files_to_ignore = 
  foo.py,
  bar.py,
  spam.py

Then, when running flake8 on the project, it will pass because there are no files to be run on. Then we fix a single file and exclude it from the list which means it will be checked by the linter. Then process repeats for each existing file.

However, I cannot see any way to exclude all files by listing them explicitly from reading the docs. There is --exclude=<patterns> but this will ignore all files (I won't be able to filter files by names).

I have found that it is possible to ignore the whole file by adding a line # flake8: noqa on an empty line. Is it my only choice to achieve what I want?


Solution

  • There is --exclude=<patterns> but this will ignore all files (I won't be able to filter files by names).

    A single filename is a pattern, a pattern that matches just a single filename. The default value for exclude is .svn,CVS,.bzr,.hg,.git,__pycache__,.tox, which is a sequence of literal file and directory names.

    Just list your filenames as the excluded patterns, as you remove individual names from the list they'll be checked.

    flake8 first tests the base filename (no path), then the absolute path against each pattern. If your filenames are not unique (appear in multiple locations in your file tree), then prefix the files with the relative project path prefixed with */; so in the following tree

    project/
    ├── bar
    │   ├── __init__.py
    │   └── example.py
    ├── example.py
    └── foo
        ├── __init__.py
        └── example.py
    

    the patterns */project/example.py, */project/foo/example.py and */project/bar/example.py match the 3 individual example.py files exactly.