Flake8 - line break before binary operator - how to fix it?
Asked Answered
C

2

6

I keep getting:

W503 line break before binary operator

Please help me fix my code, as I can't figure out what is wrong here:

def check_actionable(self, user_name, op, changes_table):
        return any(user_name in row.inner_text() and row.query_selector(
            self.OPS[op]) is not
                   None and row.query_selector(self.DISABLED) is not None for
                   row in changes_table)
Caledonian answered 1/9, 2022 at 10:41 Comment(1)
You might wanna simplify that function into more than a one-liner. I's pretty unreadable. The flake8 warning is just a symptom of that.Expecting
M
16

the other (imo better) alternative to ignore (which resets the default ignore list) is to use extend-ignore

by default both W503 and W504 are ignored (as they conflict and have flip-flopped historically). there are other rules which are ignored by default as well that you may want to preserve

extend-ignore = ABC123

ignore on the other hand resets the ignore list removing the defaults


disclaimer: I'm the current flake8 maintainer

Misnomer answered 1/9, 2022 at 12:36 Comment(2)
thx for your additional information. I don't know you can set extend-ignore field in .flake8 before.Carpi
This helped me realize that the reason I started getting W503 and W504 was because I added an ignore for a different error, which reset the default ignore list.Taught
C
7

W503 rule and W504 rule of flake8 are conflicted to each other. I recommend you to add one of them into your .flake8's ignore list.

W503: line break before binary operator

W504: line break after binary operator

ignore = D400,D300,D205,D200,D105,D100,D101,D103,D107,W503,E712

The below code is prettified:

def check_actionable(self, user_name, op, changes_table):
    return any(user_name in row.inner_text() and
               row.query_selector(self.OPS[op]) is not None and
               row.query_selector(self.DISABLED) is not None for row in changes_table)

Some explanation on W503 and W504:

binary operator: +, -, /, and, or, ...

To pass W503, your code should be like this:

x = (1
     + 2)

To pass W504, your code should be like this:

x = (1 +
     2)
Carpi answered 1/9, 2022 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.