Skip to content

Commit

Permalink
support fix e722 (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
hhatto committed Feb 9, 2017
1 parent 9393f3c commit dee9970
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.rst
Expand Up @@ -214,6 +214,7 @@ autopep8 fixes the following issues_ reported by pycodestyle_::
E711 - Fix comparison with None.
E712 - Fix comparison with boolean.
E721 - Use "isinstance()" instead of comparing types directly.
E722 - Fix bare except.
W291 - Remove trailing whitespace.
W293 - Remove trailing whitespace on blank line.
W391 - Remove trailing blank lines.
Expand Down
12 changes: 11 additions & 1 deletion autopep8.py
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')
BARE_EXCEPT_REGEX = re.compile(r"except\s*:")


# For generating line shortening candidates.
Expand Down Expand Up @@ -406,6 +407,7 @@ class FixPEP8(object):
- e502
- e701,e702
- e711,e712,e713,e714
- e722
- e731
- w291
- w503
Expand Down Expand Up @@ -1014,6 +1016,14 @@ def fix_e714(self, result):
target[:pos_start], match.group(2), match.group(3),
match.group(1), target[match.end():])

def fix_e722(self, result):
"""fix bare except"""
(line_index, _, target) = get_index_offset_contents(result,
self.source)
if BARE_EXCEPT_REGEX.search(target):
self.source[line_index] = '{0}{1}'.format(
target[:result['column'] - 1], "except Exception:")

def fix_e731(self, result):
"""Fix do not assign a lambda expression check."""
(line_index, _, target) = get_index_offset_contents(result,
Expand Down Expand Up @@ -2786,7 +2796,7 @@ def filter_results(source, results, aggressive):
continue

if aggressive <= 0:
if issue_id.startswith(('e711', 'w6')):
if issue_id.startswith(('e711', 'e72', 'w6')):
continue

if aggressive <= 1:
Expand Down
11 changes: 11 additions & 0 deletions test/test_autopep8.py
Expand Up @@ -3890,6 +3890,17 @@ def test_e721_in_conditional(self):
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)

def test_e722(self):
line = "try:\n print(a)\nexcept:\n pass\n"
fixed = "try:\n print(a)\nexcept Exception:\n pass\n"
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)

def test_e722_non_aggressive(self):
line = "try:\n print(a)\nexcept:\n pass\n"
with autopep8_context(line, options=[]) as result:
self.assertEqual(line, result)

def test_e731(self):
line = 'a = lambda x: x * 2\n'
fixed = 'def a(x): return x * 2\n'
Expand Down

0 comments on commit dee9970

Please sign in to comment.