如何格式化此代码以使 flake8 满意?

How to format this code so that flake8 is happy?

此代码由 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 line break before binary operator

tests/test_utils.py:59:101: E501 line too long (105 > 100 characters)

如何格式化上面的行并使 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

tox.ini:

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

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

根据您的配置,您已设置 ignore = W504

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

如果您删除 ignore=W504W503 都处于默认忽略状态,因此不会被捕获

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

对于黑色,这是 suggested configuration:

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

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


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