diff --git a/autopep8.py b/autopep8.py index 307c6711..70d63ab7 100755 --- a/autopep8.py +++ b/autopep8.py @@ -1251,6 +1251,10 @@ def fix_w503(self, result): tts = ts old = None for t in tts: + if t[0] in (tokenize.NEWLINE, tokenize.NL): + newline_count -= 1 + if newline_count <= 1: + break if tokenize.COMMENT == t[0] and old and old[0] != tokenize.NL: comment_index = old[3][1] break diff --git a/test/test_autopep8.py b/test/test_autopep8.py index 3ad067f6..26846598 100755 --- a/test/test_autopep8.py +++ b/test/test_autopep8.py @@ -4302,6 +4302,24 @@ def test_w503_with_comment(self): with autopep8_context(line, options=['--select=W503']) as result: self.assertEqual(fixed, result) + def test_w503_with_comment_into_point_out_line(self): + line = """\ +def test(): + return ( + True not in [] + and False # comment required + ) +""" + fixed = """\ +def test(): + return ( + True not in [] and + False # comment required + ) +""" + with autopep8_context(line, options=['--select=W503']) as result: + self.assertEqual(fixed, result) + def test_w503_with_comment_double(self): line = """\ (