Skip to content

Commit

Permalink
Merge pull request #748 from hhatto/fix-issue688
Browse files Browse the repository at this point in the history
fix specific case of e271 and w504
  • Loading branch information
hhatto authored May 29, 2024
2 parents f028951 + 765402a commit 83e29b6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions test/test_autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 83e29b6

Please sign in to comment.