diff --git a/autopep8.py b/autopep8.py index e3666bc9..437c92f7 100755 --- a/autopep8.py +++ b/autopep8.py @@ -477,6 +477,7 @@ def __init__(self, filename, self.source = sio.readlines() self.options = options self.indent_word = _get_indentword(''.join(self.source)) + self.original_source = copy.copy(self.source) # collect imports line self.imports = {} @@ -528,6 +529,13 @@ def __init__(self, filename, self.fix_w292 = self.fix_w291 self.fix_w293 = self.fix_w291 + def _check_affected_anothers(self, result) -> bool: + """Check if the fix affects the number of lines of another remark.""" + line_index = result['line'] - 1 + target = self.source[line_index] + original_target = self.original_source[line_index] + return target != original_target + def _fix_source(self, results): try: (logical_start, logical_end) = _find_logical(self.source) @@ -561,8 +569,12 @@ def _fix_source(self, results): completed_lines): continue + if self._check_affected_anothers(result): + continue modified_lines = fix(result, logical) else: + if self._check_affected_anothers(result): + continue modified_lines = fix(result) if modified_lines is None: diff --git a/test/test_autopep8.py b/test/test_autopep8.py index 3d591d52..bf73fa08 100755 --- a/test/test_autopep8.py +++ b/test/test_autopep8.py @@ -2256,6 +2256,18 @@ def test_e271_with_multiline(self): with autopep8_context(line) as result: self.assertEqual(fixed, result) + def test_e271_and_w504_with_affects_another_result_line(self): + line = """\ +cm_opts = ([1] + + [d for d in [3,4]]) +""" + fixed = """\ +cm_opts = ([1] + + [d for d in [3,4]]) +""" + with autopep8_context(line, options=["--select=E271,W504"]) as result: + self.assertEqual(fixed, result) + def test_e272(self): line = 'True and False\n' fixed = 'True and False\n'