2

此代码由black创建:

def test_schema_org_script_from_list():
    assert (
        schema_org_script_from_list([1, 2])
        == '<script type="application/ld+json">1</script>\n<script type="application/ld+json">2</script>'
    )

但现在 flake8 抱怨:

tests/test_utils.py:59:9: W503 换行符在二元运算符之前

测试/test_utils.py:59:101:E501 行太长(105 > 100 个字符)

如何格式化以上行并使 flake8 高兴?

我用这个.pre-commit-config.yaml

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
  - repo: 'https://github.com/pre-commit/pre-commit-hooks'
    rev: v3.2.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-added-large-files
  - repo: 'https://gitlab.com/pycqa/flake8'
    rev: 3.8.4
    hooks:
      - id: flake8
  - repo: 'https://github.com/pre-commit/mirrors-isort'
    rev: v5.7.0
    hooks:
      - id: isort

毒物.ini:

[flake8]
max-line-length = 100
exclude = .git,*/migrations/*,node_modules,migrate
# W504 line break after binary operator
ignore = W504

(我认为 flake8 从属于不同工具的文件中读取配置有点奇怪)。

4

1 回答 1

7

从您的配置中,您已设置ignore = W504

ignore不是您想要的选项,因为它会重置默认忽略(带来一堆东西,包括 W503)。

如果您删除ignore=,则两者W504W503都默认忽略,因此不会被捕获

至于你的E501(线太长),你可以extend-ignore = E501或者你可以max-line-length适当设置

对于黑色,这是建议的配置

[flake8]
max-line-length = 88
extend-ignore = E203

请注意,在某些情况下,黑色无法使一行足够短(如您所见)——来自长字符串和长变量名


免责声明:我是当前的 flake8 维护者

于 2021-01-26T20:35:08.950 回答