diff --git a/autopep8.py b/autopep8.py index 2ad359fb..23f4ab60 100755 --- a/autopep8.py +++ b/autopep8.py @@ -67,6 +67,7 @@ class documentation for more information. from ConfigParser import Error import pycodestyle +from pycodestyle import STARTSWITH_INDENT_STATEMENT_REGEX try: @@ -1028,7 +1029,13 @@ def fix_e702(self, result, logical): # https://docs.python.org/reference/compound_stmts.html for line in logical_lines: if (result['id'] == 'E702' and ':' in line - and STARTSWITH_DEF_REGEX.match(line)): + and STARTSWITH_INDENT_STATEMENT_REGEX.match(line)): + if self.options.verbose: + print( + '---> avoid fixing {error} with ' + 'other compound statements'.format(error=result['id']), + file=sys.stderr + ) return [] line_index = result['line'] - 1 diff --git a/test/test_autopep8.py b/test/test_autopep8.py index 56083994..5001f8f4 100755 --- a/test/test_autopep8.py +++ b/test/test_autopep8.py @@ -4017,6 +4017,15 @@ def test_e702_with_dict_semicolon(self): with autopep8_context(line) as result: self.assertEqual(fixed, result) + def test_e702_with_e701_and_only_select_e702_option(self): + line = """\ +for i in range(3): + if i == 1: print i; continue + print i +""" + with autopep8_context(line, options=["--select=E702"]) as result: + self.assertEqual(line, result) + def test_e703_with_inline_comment(self): line = 'a = 5; # inline comment\n' fixed = 'a = 5 # inline comment\n'