Skip to content

Commit

Permalink
Merge 986469d into 465c0da
Browse files Browse the repository at this point in the history
  • Loading branch information
hhatto committed Sep 3, 2018
2 parents 465c0da + 986469d commit 0bf1b80
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 12 deletions.
63 changes: 58 additions & 5 deletions autopep8.py
Expand Up @@ -94,7 +94,7 @@ class documentation for more information.
])


DEFAULT_IGNORE = 'E226,E24,W503,W690' # TODO: use pycodestyle.DEFAULT_IGNORE
DEFAULT_IGNORE = 'E226,E24,W50,W690' # TODO: use pycodestyle.DEFAULT_IGNORE
DEFAULT_INDENT_SIZE = 4

SELECTED_GLOBAL_FIXED_METHOD_CODES = ['W602', ]
Expand Down Expand Up @@ -418,7 +418,7 @@ class FixPEP8(object):
- e722
- e731
- w291
- w503
- w503,504
"""

Expand Down Expand Up @@ -1231,6 +1231,59 @@ def fix_w503(self, result):
self.source[line_index - 1] = '{} {}{}'.format(
before_line[:bl], one_string_token, before_line[bl:])

def fix_w504(self, result):
(line_index, _, target) = get_index_offset_contents(result,
self.source)
# NOTE: is not collect pointed out in pycodestyle==2.4.0
comment_index = 0
operator_position = None # (start_position, end_position)
for i in range(1, 6):
to_index = line_index + i
try:
ts = generate_tokens("".join(self.source[line_index:to_index]))
except (SyntaxError, tokenize.TokenError):
continue
newline_count = 0
newline_index = []
for index, t in enumerate(ts):
if t[0] == tokenize.OP:
if t[2][0] == 1 and t[3][0] == 1:
operator_position = (t[2][1], t[3][1])
elif t[0] in (tokenize.NEWLINE, tokenize.NL):
newline_index.append(index)
newline_count += 1
if newline_count > 2:
tts = ts[:newline_index[-3]]
else:
tts = ts
old = []
for t in tts:
if tokenize.COMMENT == t[0] and old:
comment_index = old[3][1]
break
old = t
break
if not operator_position:
return
target_operator = target[operator_position[0]:operator_position[1]]
if comment_index:
self.source[line_index] = '{}{}'.format(
target[:operator_position[0]].rstrip(),
target[comment_index:])
else:
self.source[line_index] = '{}{}{}'.format(
target[:operator_position[0]].rstrip(),
target[operator_position[1]:].lstrip(),
target[operator_position[1]:])
next_line = self.source[line_index + 1]
next_line_indent = 0
m = re.match(r'\s*', next_line)
if m:
next_line_indent = m.span()[1]
self.source[line_index + 1] = '{}{} {}'.format(
next_line[:next_line_indent], target_operator,
next_line[next_line_indent:])

def fix_w605(self, result):
(line_index, _, target) = get_index_offset_contents(result,
self.source)
Expand Down Expand Up @@ -3014,11 +3067,11 @@ def filter_results(source, results, aggressive):
continue

if aggressive <= 1:
if issue_id.startswith(('e712', 'e713', 'e714', 'w5')):
if issue_id.startswith(('e712', 'e713', 'e714')):
continue

if aggressive <= 2:
if issue_id.startswith(('e704', 'w5')):
if issue_id.startswith(('e704')):
continue

if r['line'] in commented_out_code_line_numbers:
Expand Down Expand Up @@ -3500,7 +3553,7 @@ def parse_args(arguments, apply_config=False):
elif not args.select:
if args.aggressive:
# Enable everything by default if aggressive.
args.select = {'E', 'W'}
args.select = {'E', 'W1', 'W2', 'W3', 'W6'}
else:
args.ignore = _split_comma_separated(DEFAULT_IGNORE)

Expand Down
38 changes: 31 additions & 7 deletions test/test_autopep8.py
Expand Up @@ -1561,8 +1561,12 @@ def extractBlocks(self):
"""
fixed = """\
def extractBlocks(self):
addLine = (self.matchMultiple(linesIncludePatterns, line) and
not self.matchMultiple(linesExcludePatterns, line)) or emptyLine
addLine = (
self.matchMultiple(
linesIncludePatterns,
line) and not self.matchMultiple(
linesExcludePatterns,
line)) or emptyLine
"""
with autopep8_context(line, options=['-aaa']) as result:
self.assertEqual(fixed, result)
Expand Down Expand Up @@ -4266,7 +4270,7 @@ def test_w391_more_complex(self):
def test_w503(self):
line = '(width == 0\n + height == 0)\n'
fixed = '(width == 0 +\n height == 0)\n'
with autopep8_context(line, options=['-aaa']) as result:
with autopep8_context(line, options=['--select=W503']) as result:
self.assertEqual(fixed, result)

def test_w503_skip_default(self):
Expand All @@ -4277,13 +4281,13 @@ def test_w503_skip_default(self):
def test_w503_and_or(self):
line = '(width == 0\n and height == 0\n or name == "")\n'
fixed = '(width == 0 and\n height == 0 or\n name == "")\n'
with autopep8_context(line, options=['-aaa']) as result:
with autopep8_context(line, options=['--select=W503']) as result:
self.assertEqual(fixed, result)

def test_w503_with_comment(self):
line = '(width == 0 # this is comment\n + height == 0)\n'
fixed = '(width == 0 + # this is comment\n height == 0)\n'
with autopep8_context(line, options=['-aaa']) as result:
with autopep8_context(line, options=['--select=W503']) as result:
self.assertEqual(fixed, result)

def test_w503_with_comment_double(self):
Expand All @@ -4301,7 +4305,7 @@ def test_w503_with_comment_double(self):
333333333333 # C3
)
"""
with autopep8_context(line, options=['-aaa']) as result:
with autopep8_context(line, options=['--select=W503']) as result:
self.assertEqual(fixed, result)

def test_w503_over_5lines(self):
Expand All @@ -4327,7 +4331,27 @@ def test_w503_over_5lines(self):
7 # 7
)
"""
with autopep8_context(line, options=['-aaa']) as result:
with autopep8_context(line, options=['--select=W503']) as result:
self.assertEqual(fixed, result)

@unittest.skip('TODO')
def test_w503_with_line_comment(self):
line = '(width == 0\n # this is comment\n + height == 0)\n'
fixed = '(width == 0 +\n # this is comment\n height == 0)\n'
with autopep8_context(line, options=['--select=W503', '--ignore=E']) as result:
self.assertEqual(fixed, result)

def test_w504(self):
line = '(width == 0 +\n height == 0)\n'
fixed = '(width == 0\n + height == 0)\n'
with autopep8_context(line, options=['--select=W504', '--ignore=E']) as result:
self.assertEqual(fixed, result)

@unittest.skip('TODO')
def test_w504_with_line_comment(self):
line = '(width == 0 +\n # this is comment\n height == 0)\n'
fixed = '(width == 0\n # this is comment\n + height == 0)\n'
with autopep8_context(line, options=['--select=W504', '--ignore=E']) as result:
self.assertEqual(fixed, result)

def test_w601(self):
Expand Down

0 comments on commit 0bf1b80

Please sign in to comment.