5

I have a condition that reads like:

ok = (not a > 10 and
      not b < 10 and
      not c > 99 and
      d == 99)

flake8 complains about this line with the error message:

W504 line break after binary operator

When I move the operator around, it throws a different error:

ok = (not a > 10
      and not b < 10
      and not c > 99
      and d == 99)

W503 line break before binary operator

I tried multiple recommendations (e.g., this), but still, flake8 complains about the linebreak. The actual condition in my code is very long so I cannot put it in one line, also, it is the preference of my team to enclose long lines in () instead of using \.

Dr. Strangelove
  • 2,725
  • 3
  • 34
  • 61
  • 1
    you need to disable either `W504` or `W503` in your flake8 config, it's impossible to use both at the same time if you're not using `/\` – frozen Jun 11 '21 at 18:54
  • [Black also introduces such line brakes](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwi59eD8oJDxAhWO7Z4KHe13BoAQFjAAegQIBhAD&url=https%3A%2F%2Fblack.vercel.app%2F&usg=AOvVaw04R2nfqXBKHRytLZ482Js-), I guess without disabling any of errors. So, I was wondering maybe there is a way to pass flake8 without disabling any of the warnings. – Dr. Strangelove Jun 11 '21 at 18:59
  • 2
    2 comments not directly related to the question you asked but might help - firstly, when checking whether multiple conditionals are true, you may want to use [`all`](https://docs.python.org/3.8/library/functions.html#all) eg: `ok = all([not a > 10, not b < 10 ...]):` . Secondly, a `not conditional and not conditional` is the same as `not (conditional or conditional)` according to [De Morgan's Law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws) . Without checking it, I suspect what's tripping up flake8 are the 2 binary operators. Why not try `any` : `ok = not any([a > 10, b < 10, ...])` – lonetwin Jun 11 '21 at 19:05
  • Thanks for the suggestion @lonetwin. The condition I wrote above merely a dummy example. The actual condition I've is much more complicated and I will use your suggestions to improve. – Dr. Strangelove Jun 11 '21 at 19:07

2 Answers2

14

you have set ignore = in your configuration -- you should use extend-ignore =

W504 and W503 conflict with each other (and are both disabled by default) -- by setting ignore you've re-enabled them. extend-ignore does not have this problem as it augments the default set of ignored codes


disclaimer: I'm the current flake8 maintainer

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
-5
ok = (
    not a > 10 and
    not b < 10 and
    not c > 99 and
    d == 99
)

This should resolve the issue.

Paras Gupta
  • 264
  • 3
  • 11