summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Stapleton Cordasco <graffatcolmingov@gmail.com>2019-09-25 17:47:26 -0500
committerGitHub <noreply@github.com>2019-09-25 17:47:26 -0500
commitb93125aa67ce201e9194445472fcf54a5a925db9 (patch)
tree341de031329a799d6b79d7482a35de9dc24cd938
parent9726e10004e5615c432f126530ee0540b9903f6e (diff)
parentdd6268c488ec6ab23188f31f7a1d345a8e114cbe (diff)
downloadpep8-b93125aa67ce201e9194445472fcf54a5a925db9.tar.gz
Merge pull request #834 from EricCousineau-TRI/issue/833
E402: Add "with" statement to allowed keywords
-rwxr-xr-xpycodestyle.py9
-rw-r--r--testsuite/E40.py15
2 files changed, 20 insertions, 4 deletions
diff --git a/pycodestyle.py b/pycodestyle.py
index 4ade087..6ad5456 100755
--- a/pycodestyle.py
+++ b/pycodestyle.py
@@ -1073,7 +1073,8 @@ def module_imports_on_top_of_file(
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', 'elif')
if indent_level: # Allow imports in conditional statement/function
return
@@ -1087,9 +1088,9 @@ def module_imports_on_top_of_file(
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
diff --git a/testsuite/E40.py b/testsuite/E40.py
index f9a18fc..6c71fa2 100644
--- a/testsuite/E40.py
+++ b/testsuite/E40.py
@@ -34,6 +34,21 @@ finally:
print('made attempt to import foo')
import bar
+#: Okay
+with warnings.catch_warnings():
+ warnings.filterwarnings("ignore", DeprecationWarning)
+ import foo
+
+import bar
+#: Okay
+if False:
+ import foo
+elif not True:
+ import bar
+else:
+ import mwahaha
+
+import bar
#: E402
VERSION = '1.2.3'