diff --git a/autopep8.py b/autopep8.py index feb46634..9efd3b41 100755 --- a/autopep8.py +++ b/autopep8.py @@ -1295,8 +1295,14 @@ def fix_w503(self, result): before_line[:comment_index], one_string_token, before_line[comment_index + 1:]) else: - self.source[fix_target_line] = '{} {}{}'.format( - before_line[:bl], one_string_token, before_line[bl:]) + if before_line[:bl].endswith("#"): + # special case + # see: https://github.com/hhatto/autopep8/issues/503 + self.source[fix_target_line] = '{}{} {}'.format( + before_line[:bl-2], one_string_token, before_line[bl-2:]) + else: + self.source[fix_target_line] = '{} {}{}'.format( + before_line[:bl], one_string_token, before_line[bl:]) def fix_w504(self, result): (line_index, _, target) = get_index_offset_contents(result, diff --git a/test/test_autopep8.py b/test/test_autopep8.py index 5f6e0213..8cb9b605 100755 --- a/test/test_autopep8.py +++ b/test/test_autopep8.py @@ -4452,6 +4452,22 @@ def test_w503_with_comment_double(self): 22222222 and # C2 333333333333 # C3 ) +""" + with autopep8_context(line, options=['--select=W503']) as result: + self.assertEqual(fixed, result) + + def test_w503_with_comment_with_only_comment_block_charactor(self): + line = """\ +if (True # + and True + and True): + print(1) +""" + fixed = """\ +if (True and # + True and + True): + print(1) """ with autopep8_context(line, options=['--select=W503']) as result: self.assertEqual(fixed, result)