yapf
yapf copied to clipboard
Lengthy 'if not' conditional causes E901 SyntaxError
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
Nice catch! Thanks for the suggested fix. :-)
:+1: