Skip to content

Commit 1f8d5ca

Browse files
E402: Add "with" statement to allowed keywords
1 parent ac1c5e5 commit 1f8d5ca

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

pycodestyle.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ def is_string_literal(line):
10671067
line = line[1:]
10681068
return line and (line[0] == '"' or line[0] == "'")
10691069

1070-
allowed_try_keywords = ('try', 'except', 'else', 'finally')
1070+
allowed_keywords = ('try', 'except', 'else', 'finally', 'with')
10711071

10721072
if indent_level: # Allow imports in conditional statement/function
10731073
return
@@ -1081,9 +1081,9 @@ def is_string_literal(line):
10811081
yield 0, "E402 module level import not at top of file"
10821082
elif re.match(DUNDER_REGEX, line):
10831083
return
1084-
elif any(line.startswith(kw) for kw in allowed_try_keywords):
1085-
# Allow try, except, else, finally keywords intermixed with
1086-
# imports in order to support conditional importing
1084+
elif any(line.startswith(kw) for kw in allowed_keywords):
1085+
# Allow certain keywords intermixed with imports in order to
1086+
# support conditional or filtered importing
10871087
return
10881088
elif is_string_literal(line):
10891089
# The first literal is a docstring, allow it. Otherwise, report

testsuite/E40.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
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+
3642
import bar
3743
#: E402
3844
VERSION = '1.2.3'

0 commit comments

Comments
 (0)