yapf icon indicating copy to clipboard operation
yapf copied to clipboard

Lengthy 'if not' conditional causes E901 SyntaxError

Open affablebloke opened this issue 11 years ago • 2 comments

After running YAPF on a large codebase, I discovered that lengthy 'if not' conditionals were causing E901 SyntaxErrors. However, this could be mitigated by modifying the code after the or to be wrapped with parentheses.

Example:

def test():
    if not "Foooooooooooooooooooooooooooooo" or "Foooooooooooooooooooooooooooooo" == "Foooooooooooooooooooooooooooooo":
        pass

Reformatted using YAPF:

def test():
    if not
    "Foooooooooooooooooooooooooooooo" or "Foooooooooooooooooooooooooooooo" == "Foooooooooooooooooooooooooooooo":
        pass

Modifying _unwrapped_line.SplitPenalty() seems to fix the issue and the tests still pass.

From _SplitPenalty line 392:

if prev_token.value == 'not':
    return split_penalty.STRONGLY_CONNECTED

To:

if prev_token.value == 'not':
    return split_penalty.UNBREAKABLE

affablebloke avatar Jul 01 '15 15:07 affablebloke

Nice catch! Thanks for the suggested fix. :-)

bwendling avatar Jul 02 '15 21:07 bwendling

:+1:

affablebloke avatar Jul 02 '15 22:07 affablebloke