使用预提交排除黑色运行的某些文件

问题描述 投票:0回答:1

我想在预提交中配置黑色,并从检查任何迁移文件夹中排除预提交。

我的pyproject.toml看起来像这样

[tool.black]
line-length = 79
target-version = ['py37']
include = '\.pyi?$'
exclude = '''

(
  /(
      \.eggs
    | \.git
    | \.hg
    | \.mypy_cache
    | \.tox
    | \.venv
    | _build
    | buck-out
    | build
    | dist
  )/
  | ^migrations/
'''

我还配置了预提交。但是在运行pre-commit run --all-files时黑色也会格式化迁移文件夹我该如何配置黑色

pre-commit pre-commit.com python-black
1个回答
0
投票

this issue问题跟踪器上的[black概述了您的特定问题

[pre-commit查找所有python文件,然后应用pre-commit的排除项,然后将该文件列表传递给基础工具(在本例中为black)]

[black当前(在撰写本文时)将格式化命令行上列出的所有文件-与blackexclude模式无关

建议使用pre-commit的排除(通过您的.pre-commit-config.yaml),以使这些文件根本不会传递为黑色:

-   id: black
    exclude: ^migrations/

note:与黑色不同,pre-commit仅会检查进入git存储库的皮棉文件,因此这里不需要排除洗涤清单(.git / .mypy_cache等)


免责声明:我是pre-commit的作者>

© www.soinside.com 2019 - 2024. All rights reserved.