black 和 flake8 钩子冲突

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

黑色失败并重新格式化以下

foo1 = foo2[len(foo3):]

foo1 = foo2[len(foo3) :]

但是 Flake8 失败了

foo1 = foo2[len(foo3) :]

并且想要

foo1 = foo2[len(foo3):]

除非我弄错了,在这种情况下Flake8是对的。

在这种情况下,如何进行不会因 black 和 flake8 而失败的预提交?

pre-commit-hook flake8
2个回答
2
投票

感谢 Anthony Sottile 的回答,我们可以这样说:

1- Black 希望为“复杂表达式”提供额外的空间,如 foo1 = foo2[len(foo3) :],这种行为可能会在 Flake8 等样式指南强制工具中引发“:”警告之前的 E203 空白。由于 E203 不符合 PEP 8,我们应该告诉 Flake8 忽略这些警告。

2-与我的想法相反,pyproject.toml 文件没有用于 flake8 的配置。 我们必须绝对且排他地使用.flake8、setup.cfg 或 tox.ini。

最后,这是我使用的配置(它解决了最初遇到的问题):

$ more .pre-commit-config.yaml
repos:

-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0
    hooks: # general hooks
    -   id: check-added-large-files
    -   id: check-ast
    -   id: check-case-conflict
    -   id: check-docstring-first
    -   id: check-json
    -   id: check-merge-conflict
    -   id: check-toml
    -   id: check-xml
    -   id: check-yaml
    -   id: debug-statements
    -   id: end-of-file-fixer
    -   id: fix-encoding-pragma
    -   id: name-tests-test
    -   id: trailing-whitespace

-   repo: https://github.com/psf/black
    rev: 23.1.0
    hooks: # code formatter
    -   id: black
        language: python
        args: ["--line-length=79"]

-   repo: https://github.com/pycqa/isort
    rev: 5.12.0
    hooks: # imports sorting
    -   id: isort
        name: isort (python)

-   repo: https://github.com/econchick/interrogate
    rev: 1.5.0
    hooks: # documentation checker
    -   id: interrogate
        args: [--fail-under=80, -vv]

-   repo: https://github.com/PyCQA/flake8
    rev: 6.0.0
    hooks:
    -   id: flake8
        name: flake8 under python3
        language_version: python3

$ more pyproject.toml
[tool.black]
line-length = 79
include = '\.pyi?$'
exclude = '''
/(
    \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
)/
'''

#[tool.flake8]
#max-line-length = 79
#exclude = [".git", "__pycache__", "dist"]
#extend-ignore = ["E203", "E266", "E501", "W503", "F403", "F401"]
#max-complexity = 10

[tool.isort]
atomic = true
profile = "black"
line_length = 79
skip_gitignore = true

$ more .flake8
[flake8]
ignore = E203, E266, E501, W503, F403, F401
exclude =
    .git,
    __pycache__,
    docs/source/conf.py,
    build,
    dist
max-complexity = 10
max-line-length = 79

0
投票

这个问题在here讨论,是老flake8的问题。 我将 flake8 的版本更新到最新版本,它对我有用。

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