【问题标题】:Run different pre-commit hooks on different lists of files在不同的文件列表上运行不同的预提交挂钩
【发布时间】:2022-12-15 13:06:15
【问题描述】:

我正在尝试逐步向遗留代码库添加 linting 和自动代码格式化。

现在,我只是在同一个“已清理”文件列表上运行所有挂钩。

这是我的.pre-commit-config.yaml 文件:

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

我想更改此设置,以便在所有文件上运行isortblack,并且基于白名单的清理文件选择仅适用于flake8

有没有办法做到这一点?我尝试将 files 块移动到 flake8 配置的一个小节,但出现以下错误:

[WARNING] Unexpected key(s) present on https://github.com/pycqa/flake8: files

【问题讨论】:

    标签: static-analysis pre-commit-hook


    【解决方案1】:

    我找到了答案:可以为每个挂钩独立完成文件选择

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

    我的错误是由错误的缩进引起的。我最初尝试将 files 添加到与 hooksreporev 相同的级别,当它们需要成为 hooks 的子参数时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 2013-07-21
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      相关资源
      最近更新 更多