Skip to content

Commit b93125a

Browse files
authored
Merge pull request #834 from EricCousineau-TRI/issue/833
E402: Add "with" statement to allowed keywords
2 parents 9726e10 + dd6268c commit b93125a

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

pycodestyle.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,8 @@ def is_string_literal(line):
10731073
line = line[1:]
10741074
return line and (line[0] == '"' or line[0] == "'")
10751075

1076-
allowed_try_keywords = ('try', 'except', 'else', 'finally')
1076+
allowed_keywords = (
1077+
'try', 'except', 'else', 'finally', 'with', 'if', 'elif')
10771078

10781079
if indent_level: # Allow imports in conditional statement/function
10791080
return
@@ -1087,9 +1088,9 @@ def is_string_literal(line):
10871088
yield 0, "E402 module level import not at top of file"
10881089
elif re.match(DUNDER_REGEX, line):
10891090
return
1090-
elif any(line.startswith(kw) for kw in allowed_try_keywords):
1091-
# Allow try, except, else, finally keywords intermixed with
1092-
# imports in order to support conditional importing
1091+
elif any(line.startswith(kw) for kw in allowed_keywords):
1092+
# Allow certain keywords intermixed with imports in order to
1093+
# support conditional or filtered importing
10931094
return
10941095
elif is_string_literal(line):
10951096
# The first literal is a docstring, allow it. Otherwise, report

testsuite/E40.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@
3333
finally:
3434
print('made attempt to import foo')
3535

36+
import bar
37+
#: Okay
38+
with warnings.catch_warnings():
39+
warnings.filterwarnings("ignore", DeprecationWarning)
40+
import foo
41+
42+
import bar
43+
#: Okay
44+
if False:
45+
import foo
46+
elif not True:
47+
import bar
48+
else:
49+
import mwahaha
50+
3651
import bar
3752
#: E402
3853
VERSION = '1.2.3'

0 commit comments

Comments
 (0)