Flake8 – line break before binary operator – how to fix it?

Question:

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)
Asked By: Tal Angel

||

Answers:

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)
Answered By: Will Zhao

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

Answered By: Anthony Sottile
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.