Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix w292 with ignore option #608

Merged
merged 6 commits into from Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 24 additions & 7 deletions autopep8.py
Expand Up @@ -531,6 +531,7 @@ def __init__(self, filename,
options and (options.aggressive >= 2 or options.experimental) else
self.fix_long_line_physically)
self.fix_e703 = self.fix_e702
self.fix_w292 = self.fix_w291
self.fix_w293 = self.fix_w291

def _fix_source(self, results):
Expand Down Expand Up @@ -1737,9 +1738,15 @@ def refactor(source, fixer_names, ignore=None, filename=''):
Skip if ignore string is produced in the refactored code.

"""
not_found_end_of_file_newline = source and source.rstrip("\r\n") == source
if not_found_end_of_file_newline:
input_source = source + "\n"
else:
input_source = source

from lib2to3 import pgen2
try:
new_text = refactor_with_2to3(source,
new_text = refactor_with_2to3(input_source,
fixer_names=fixer_names,
filename=filename)
except (pgen2.parse.ParseError,
Expand All @@ -1752,6 +1759,9 @@ def refactor(source, fixer_names, ignore=None, filename=''):
if ignore in new_text and ignore not in source:
return source

if not_found_end_of_file_newline:
return new_text.rstrip("\r\n")

return new_text


Expand Down Expand Up @@ -2990,9 +3000,11 @@ def full_error_results(self):
return checker.report.full_error_results()


def _remove_leading_and_normalize(line):
def _remove_leading_and_normalize(line, with_rstrip=True):
# ignore FF in first lstrip()
return line.lstrip(' \t\v').rstrip(CR + LF) + '\n'
if with_rstrip:
return line.lstrip(' \t\v').rstrip(CR + LF) + '\n'
return line.lstrip(' \t\v')


class Reindenter(object):
Expand All @@ -3019,8 +3031,11 @@ def __init__(self, input_text):
self.lines.append(line)
else:
# Only expand leading tabs.
self.lines.append(_get_indentation(line).expandtabs() +
_remove_leading_and_normalize(line))
with_rstrip = line_number != len(source_lines)
self.lines.append(
_get_indentation(line).expandtabs() +
_remove_leading_and_normalize(line, with_rstrip)
)

self.lines.insert(0, None)
self.index = 1 # index into self.lines of next line
Expand Down Expand Up @@ -3448,9 +3463,11 @@ def normalize_line_endings(lines, newline):
"""Return fixed line endings.

All lines will be modified to use the most common line ending.

"""
return [line.rstrip('\n\r') + newline for line in lines]
line = [line.rstrip('\n\r') + newline for line in lines]
if line and lines[-1] == lines[-1].rstrip('\n\r'):
line[-1] = line[-1].rstrip('\n\r')
return line


def mutual_startswith(a, b):
Expand Down
8 changes: 7 additions & 1 deletion test/test_autopep8.py
Expand Up @@ -4419,6 +4419,12 @@ def test_w292(self):
'--select=W292']) as result:
self.assertEqual(fixed, result)

def test_w292_ignore(self):
line = "1\n2"
with autopep8_context(line, options=['--aggressive',
'--ignore=W292']) as result:
self.assertEqual(line, result)

def test_w293(self):
line = '1\n \n2\n'
fixed = '1\n\n2\n'
Expand Down Expand Up @@ -5503,7 +5509,6 @@ def test_indent_size_is_zero(self):
def test_exit_code_with_io_error(self):
line = "import sys\ndef a():\n print(1)\n"
with readonly_temporary_file_context(line) as filename:
print(filename)
p = Popen(list(AUTOPEP8_CMD_TUPLE) + ['--in-place', filename],
stdout=PIPE, stderr=PIPE)
p.communicate()
Expand Down Expand Up @@ -5662,6 +5667,7 @@ def test_parallel_jobs_with_diff_option(self):
['--jobs=3', '--diff'], stdout=PIPE)
p.wait()
output = p.stdout.read().decode()
p.stdout.close()

actual_diffs = []
for filename in files:
Expand Down