Skip to content

Commit

Permalink
Merge branch 'master' into feature-e731
Browse files Browse the repository at this point in the history
  • Loading branch information
Hideo Hattori committed Oct 28, 2016
2 parents 95afa9c + 3a9e3e3 commit ee89cd6
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 12 deletions.
31 changes: 23 additions & 8 deletions autopep8.py
Expand Up @@ -76,6 +76,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')


# For generating line shortening candidates.
Expand Down Expand Up @@ -403,7 +404,7 @@ class FixPEP8(object):
- e401
- e502
- e701,e702
- e711
- e711,e712,e713,e714
- e731
- w291
Expand Down Expand Up @@ -979,12 +980,26 @@ def fix_e713(self, result):
(line_index, _, target) = get_index_offset_contents(result,
self.source)

# Handle very easy case only.
if re.match(r'^\s*if not [\w.]+ in [\w.]+:$', target):
self.source[line_index] = re.sub(r'if not ([\w.]+) in ([\w.]+):',
r'if \1 not in \2:',
target,
count=1)
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(
target[:pos_start], match.group(2), match.group(1),
match.group(3), target[match.end():])

def fix_e714(self, result):
"""Fix object identity should be 'is not' case."""
(line_index, _, target) = get_index_offset_contents(result,
self.source)

match = COMPARE_NEGATIVE_REGEX.search(target)
if match:
if match.group(3) == 'is':
pos_start = match.start(1)
self.source[line_index] = "{0}{1} {2} {3} {4}".format(
target[:pos_start], match.group(2), match.group(3),
match.group(1), target[match.end():])

def fix_e731(self, result):
"""Fix do not assign a lambda expression check."""
Expand Down Expand Up @@ -2737,7 +2752,7 @@ def filter_results(source, results, aggressive):
continue

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

if r['line'] in commented_out_code_line_numbers:
Expand Down
8 changes: 4 additions & 4 deletions test/suite/out/E71.py
Expand Up @@ -16,18 +16,18 @@
if X.B not in Y:
pass
#: E713
if not X in Y and Z == "zero":
if X not in Y and Z == "zero":
pass
#: E713
if X == "zero" or not Y in Z:
if X == "zero" or Y not in Z:
pass

#
#: E714
if not X is Y:
if X is not Y:
pass
#: E714
if not X.B is Y:
if X.B is not Y:
pass
#: Okay
if x not in y:
Expand Down
60 changes: 60 additions & 0 deletions test/test_autopep8.py
Expand Up @@ -3695,6 +3695,66 @@ def test_e713(self):
options=['-aa', '--select=E713']) as result:
self.assertEqual(fixed, result)

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

def test_e713_with_tuple(self):
line = """
if not role in ("domaincontroller_master",
"domaincontroller_backup",
"domaincontroller_slave",
"memberserver",
):
pass
"""
fixed = """
if role not in ("domaincontroller_master",
"domaincontroller_backup",
"domaincontroller_slave",
"memberserver",
):
pass
"""
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'
with autopep8_context(line,
options=['-aa', '--select=E714']) as result:
self.assertEqual(fixed, result)

def test_e713_and_e714(self):
line = """
if not x is y:
pass
if not role in ("domaincontroller_master",
"domaincontroller_backup",
"domaincontroller_slave",
"memberserver",
):
pass
"""
fixed = """
if x is not y:
pass
if role not in ("domaincontroller_master",
"domaincontroller_backup",
"domaincontroller_slave",
"memberserver",
):
pass
"""
with autopep8_context(line,
options=['-aa', '--select=E713,E714']) as result:
self.assertEqual(fixed, result)

def test_e721(self):
line = "type('') == type('')\n"
fixed = "isinstance('', type(''))\n"
Expand Down

0 comments on commit ee89cd6

Please sign in to comment.