Skip to content

Commit

Permalink
Merge pull request #341 from hhatto/fix-issue-276-e713-regression
Browse files Browse the repository at this point in the history
Fix issue 276 e713 regression
  • Loading branch information
hhatto committed Aug 2, 2017
2 parents ce4c633 + 9d2817f commit c380e2e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
28 changes: 24 additions & 4 deletions autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class documentation for more information.
PYTHON_SHEBANG_REGEX = re.compile(r'^#!.*\bpython[23]?\b\s*$')
LAMBDA_REGEX = re.compile(r'([\w.]+)\s=\slambda\s*([\(\)\w,\s.]*):')
COMPARE_NEGATIVE_REGEX = re.compile(r'\b(not)\s+([^][)(}{]+)\s+(in|is)\s')
COMPARE_NEGATIVE_REGEX_THROUGH = re.compile(r'\b(not\s+in)\s')
BARE_EXCEPT_REGEX = re.compile(r'except\s*:')
STARTSWITH_DEF_REGEX = re.compile(r'^(async\s+def|def)\s.*\):')

Expand Down Expand Up @@ -1046,16 +1047,35 @@ def fix_e712(self, result):

def fix_e713(self, result):
"""Fix (trivial case of) non-membership check."""
(line_index, _, target) = get_index_offset_contents(result,
self.source)
(line_index, offset, target) = get_index_offset_contents(result,
self.source)

# to convert once 'not in' -> 'in'
before_target = target[:offset]
target = target[offset:]
match_notin = COMPARE_NEGATIVE_REGEX_THROUGH.search(target)
notin_pos_start, notin_pos_end = 0, 0
if match_notin:
notin_pos_start = match_notin.start(1)
notin_pos_end = match_notin.end()
target = '{0}{1} {2}'.format(
target[:notin_pos_start], 'in', target[notin_pos_end:])

# fix 'not in'
match = COMPARE_NEGATIVE_REGEX.search(target)
if match:
if match.group(3) == 'in':
pos_start = match.start(1)
self.source[line_index] = '{0}{1} {2} {3} {4}'.format(
new_target = '{5}{0}{1} {2} {3} {4}'.format(
target[:pos_start], match.group(2), match.group(1),
match.group(3), target[match.end():])
match.group(3), target[match.end():], before_target)
if match_notin:
# revert 'in' -> 'not in'
pos_start = notin_pos_start + offset
pos_end = notin_pos_end + offset - 4 # len('not ')
new_target = '{0}{1} {2}'.format(
new_target[:pos_start], 'not in', new_target[pos_end:])
self.source[line_index] = new_target

def fix_e714(self, result):
"""Fix object identity should be 'is not' case."""
Expand Down
21 changes: 21 additions & 0 deletions test/test_autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -3851,6 +3851,27 @@ def test_e713_with_tuple(self):
options=['-aa', '--select=E713']) as result:
self.assertEqual(fixed, result)

def test_e713_chain(self):
line = 'if "@" not in x or not "/" in y:\n pass\n'
fixed = 'if "@" not in x or "/" not in y:\n pass\n'
with autopep8_context(line,
options=['-aa', '--select=E713']) as result:
self.assertEqual(fixed, result)

def test_e713_chain2(self):
line = 'if "@" not in x or "[" not in x or not "/" in y:\n pass\n'
fixed = 'if "@" not in x or "[" not in x or "/" not in y:\n pass\n'
with autopep8_context(line,
options=['-aa', '--select=E713']) as result:
self.assertEqual(fixed, result)

def test_e713_chain3(self):
line = 'if not "@" in x or "[" not in x or not "/" in y:\n pass\n'
fixed = 'if "@" not in x or "[" not in x or "/" not in y:\n pass\n'
with autopep8_context(line,
options=['-aa', '--select=E713']) as result:
self.assertEqual(fixed, result)

def test_e714(self):
line = 'if not x is y:\n pass\n'
fixed = 'if x is not y:\n pass\n'
Expand Down

0 comments on commit c380e2e

Please sign in to comment.