From 75557c061f22d52f896f8a1c732de658c41757de Mon Sep 17 00:00:00 2001 From: Hideo Hattori Date: Wed, 29 May 2024 08:28:53 +0900 Subject: [PATCH 1/3] add test for specific case of e271 and w504 --- test/test_autopep8.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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' From 91b15c58b25c8afa2b048bffdcc54f6392f96522 Mon Sep 17 00:00:00 2001 From: Hideo Hattori Date: Wed, 29 May 2024 08:32:45 +0900 Subject: [PATCH 2/3] fix: skip if fixed-method result to another result --- autopep8.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/autopep8.py b/autopep8.py index e3666bc9..da86c58e 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 = {} @@ -540,6 +541,14 @@ def _fix_source(self, results): if result['line'] in completed_lines: continue + # NOTE: Skip if the correction by the fixed-method 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] + if _target != _original_target: + continue + fixed_methodname = 'fix_' + result['id'].lower() if hasattr(self, fixed_methodname): fix = getattr(self, fixed_methodname) From 765402a0289dd4f3f8a7b56c9e36e33f77fe656b Mon Sep 17 00:00:00 2001 From: Hideo Hattori Date: Wed, 29 May 2024 08:41:21 +0900 Subject: [PATCH 3/3] fix: check just before fixed-method --- autopep8.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/autopep8.py b/autopep8.py index da86c58e..437c92f7 100755 --- a/autopep8.py +++ b/autopep8.py @@ -529,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) @@ -541,14 +548,6 @@ def _fix_source(self, results): if result['line'] in completed_lines: continue - # NOTE: Skip if the correction by the fixed-method 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] - if _target != _original_target: - continue - fixed_methodname = 'fix_' + result['id'].lower() if hasattr(self, fixed_methodname): fix = getattr(self, fixed_methodname) @@ -570,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: