0

I am trying to progressively add linting and automatic code formatting to a legacy codebase.

For now, I am just running all the hooks on the same list of "cleaned" files.

This is my .pre-commit-config.yaml file:

files: |
    (?x)^(
        mymodule/tests/.*py|
        mymodule/cleanfile1.py|
        mymodule/cleanfile2.py
    )$
repos:
-   repo: https://github.com/pycqa/isort
    rev: 5.9.3
    hooks:
    -   id: isort
        name: isort (python)
        args: [--profile=black]
    -   id: isort
        name: isort (cython)
        types: [cython]
        args: [--profile=black]
    -   id: isort
        name: isort (pyi)
        types: [pyi]
        args: [--profile=black]
-   repo: https://github.com/psf/black
    rev: 22.3.0
    hooks:
    -   id: black
-   repo: https://github.com/pycqa/flake8
    rev: 4.0.1
    hooks:
    -   id: flake8
        args:
        -   --max-line-length=88
        -   --ignore=E203,E501,E731,E741,W503,SIM106,SIM119,FS002,FS003
        additional_dependencies:
        -   flake8-comprehensions
        -   flake8-mutable
        -   flake8-simplify
        -   flake8-use-fstring

I would like to change this so that isort and black are run on all files, and the whitelist based selection of cleaned files applies only to flake8.

Is there a way to do this? I tried moving the files block to a subsection of the flake8 config, but I'm getting the following error:

[WARNING] Unexpected key(s) present on https://github.com/pycqa/flake8: files
anthony sottile
  • 61,815
  • 15
  • 148
  • 207
PiRK
  • 893
  • 3
  • 9
  • 24

1 Answers1

0

I found the answer: the files selection can be done independently for each hook

repos:
-   repo: https://github.com/pycqa/isort
    rev: 5.9.3
    hooks:
    -   id: isort
        name: isort (python)
        args: [--profile=black]
    -   id: isort
        name: isort (cython)
        types: [cython]
        args: [--profile=black]
    -   id: isort
        name: isort (pyi)
        types: [pyi]
        args: [--profile=black]
-   repo: https://github.com/psf/black
    rev: 22.3.0
    hooks:
    -   id: black
-   repo: https://github.com/pycqa/flake8
    rev: 4.0.1
    hooks:
    -   id: flake8
        args:
        -   --max-line-length=88
        -   --ignore=E203,E501,E731,E741,W503,SIM106,SIM119,FS002,FS003
        additional_dependencies:
        -   flake8-comprehensions
        -   flake8-mutable
        -   flake8-simplify
        -   flake8-use-fstring
        files: |
          (?x)^(
              mymodule/tests/.*py|
              mymodule/cleanfile1.py|
              mymodule/cleanfile2.py
          )$

My error was caused by bad indentation. I initially tried to add files at the same level as hooks, repo and rev when they need to be a subargument of hooks.

PiRK
  • 893
  • 3
  • 9
  • 24