Skip to content

Commit

Permalink
E402: Add "with" statement to allowed keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
EricCousineau-TRI committed Jan 30, 2019
1 parent ac1c5e5 commit 1f8d5ca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ def is_string_literal(line):
line = line[1:]
return line and (line[0] == '"' or line[0] == "'")

allowed_try_keywords = ('try', 'except', 'else', 'finally')
allowed_keywords = ('try', 'except', 'else', 'finally', 'with')

if indent_level: # Allow imports in conditional statement/function
return
Expand All @@ -1081,9 +1081,9 @@ def is_string_literal(line):
yield 0, "E402 module level import not at top of file"
elif re.match(DUNDER_REGEX, line):
return
elif any(line.startswith(kw) for kw in allowed_try_keywords):
# Allow try, except, else, finally keywords intermixed with
# imports in order to support conditional importing
elif any(line.startswith(kw) for kw in allowed_keywords):
# Allow certain keywords intermixed with imports in order to
# support conditional or filtered importing
return
elif is_string_literal(line):
# The first literal is a docstring, allow it. Otherwise, report
Expand Down
6 changes: 6 additions & 0 deletions testsuite/E40.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
finally:
print('made attempt to import foo')

import bar
#: Okay
with warnings.catch_warnings():
warnings.filterwarnings("ignore", DeprecationWarning)
import foo

import bar
#: E402
VERSION = '1.2.3'
Expand Down

0 comments on commit 1f8d5ca

Please sign in to comment.