6

This code was created by 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>'
    )

But now flake8 complains:

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)

How can I format above lines and make flake8 happy?

I use this .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

(I think it is a bit strange that flake8 reads config from a file which belongs to a different tool).

guettli
  • 25,042
  • 81
  • 346
  • 663
  • 3
    you need to show your flake8 configuration -- you're probably using `ignore=` when you want to use `extend-ignore=` -- the current flake8 maintainer – anthony sottile Jan 20 '21 at 16:49
  • @AnthonySottile I added my config. I use it via pre-commit.com – guettli Jan 26 '21 at 17:17
  • that's your pre-commit configuration, I need your flake8 configuration – anthony sottile Jan 26 '21 at 17:21
  • @AnthonySottile there is no flake8 config. At least I created none. And `find -name '*flake8*'` shows nothing. – guettli Jan 26 '21 at 18:43
  • there must be, the results you are showing are impossible with the defaults, try `git grep '\[flake8\]'` -- or [read the docs on how flake8 is configured](https://flake8.pycqa.org/en/latest/user/configuration.html#configuration-locations) – anthony sottile Jan 26 '21 at 18:50
  • @AnthonySottile Thank you. The root of the confusion was found now: I think it is a bit strange that flake8 reads config from a file which belongs to a different tool. – guettli Jan 26 '21 at 19:42
  • 1
    yeah well that's just how things are. a ton of tools read from setup.cfg (technically owned by distutils, yet setuptools and a bunch of others read from there), and a ton of things read from tox.ini (technically owned by tox yet a bunch of others read from there). there's proposal to add pyproject.toml which is again owned by the packaging ecosystem but a bunch of tools read from there (including isort and black). for some reason a lot of people want to put all config in one file, even if the tool doesn't own it – anthony sottile Jan 26 '21 at 20:31

1 Answers1

12

from your configuration, you've set ignore = W504

ignore isn't the option you want as it resets the default ignore (bringing in a bunch of things, including W503).

If you remove ignore=, both W504 and W503 are in the default ignore so they won't be caught

as for your E501 (line too long), you can either extend-ignore = E501 or you can set max-line-length appropriately

for black, this is the suggested configuration:

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

note that there are cases where black cannot make a line short enough (as you're seeing) -- both from long strings and from long variable names


disclaimer: I'm the current flake8 maintainer

guerda
  • 23,388
  • 27
  • 97
  • 146
anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • It's worth noting that black does not deliberately break [long lines (yet?)](https://github.com/psf/black/issues/1331). You might want to ignore `E501` to filter out "line too long" messages – floatingpurr Nov 09 '21 at 16:34