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

Add support for ignoring W191. #627

Merged
merged 6 commits into from Apr 5, 2022
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
29 changes: 21 additions & 8 deletions autopep8.py
Expand Up @@ -1661,9 +1661,9 @@ def get_item(items, index, default=None):
return default


def reindent(source, indent_size):
def reindent(source, indent_size, leave_tabs=False):
"""Reindent all lines."""
reindenter = Reindenter(source)
reindenter = Reindenter(source, leave_tabs)
return reindenter.run(indent_size)


Expand Down Expand Up @@ -3020,7 +3020,7 @@ class Reindenter(object):

"""

def __init__(self, input_text):
def __init__(self, input_text, leave_tabs=False):
sio = io.StringIO(input_text)
source_lines = sio.readlines()

Expand All @@ -3037,10 +3037,16 @@ def __init__(self, input_text):
else:
# Only expand leading tabs.
with_rstrip = line_number != len(source_lines)
self.lines.append(
_get_indentation(line).expandtabs() +
_remove_leading_and_normalize(line, with_rstrip)
)
if leave_tabs:
self.lines.append(
_get_indentation(line) +
_remove_leading_and_normalize(line, with_rstrip)
)
else:
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 @@ -3689,7 +3695,14 @@ def apply_global_fixes(source, options, where='global', filename='',
if any(code_match(code, select=options.select, ignore=options.ignore)
for code in ['E101', 'E111']):
source = reindent(source,
indent_size=options.indent_size)
indent_size=options.indent_size,
leave_tabs=not(
code_match(
'W191',
select=options.select,
ignore=options.ignore)
)
)

for (code, function) in global_fixes():
if code.upper() in SELECTED_GLOBAL_FIXED_METHOD_CODES \
Expand Down
15 changes: 13 additions & 2 deletions test/test_autopep8.py
Expand Up @@ -1810,6 +1810,16 @@ def test_w191(self):
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)

def test_w191_ignore(self):
line = """\
while True:
\tif True:
\t\t1
"""
with autopep8_context(line, options=['--aggressive', '--ignore=W191']) as result:
self.assertEqual(line, result)


def test_e131_with_select_option(self):
line = 'd = f(\n a="hello"\n "world",\n b=1)\n'
fixed = 'd = f(\n a="hello"\n "world",\n b=1)\n'
Expand Down Expand Up @@ -5826,6 +5836,7 @@ def test_parallel_jobs_with_diff_option(self):
['--jobs=3', '--diff'], stdout=PIPE)
p.wait()
output = p.stdout.read().decode()
output = output.replace("\r\n","\n") # windows compatibility
p.stdout.close()

actual_diffs = []
Expand All @@ -5834,9 +5845,9 @@ def test_parallel_jobs_with_diff_option(self):
--- original/{filename}
+++ fixed/{filename}
@@ -1 +1 @@
-'abc'
-'abc' {blank}
+'abc'
""".format(filename=filename))
""".format(filename=filename, blank="")) # in case the users IDE trims leaning whitespace
self.assertEqual(p.returncode, autopep8.EXIT_CODE_OK)
for actual_diff in actual_diffs:
self.assertIn(actual_diff, output)
Expand Down