Skip to content

Commit 52513b3

Browse files
committed
Generalize E275 to require space after all keywords, not just "import".
1 parent c9bb97f commit 52513b3

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

pycodestyle.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -480,20 +480,24 @@ def whitespace_around_keywords(logical_line):
480480

481481

482482
@register_check
483-
def missing_whitespace_after_import_keyword(logical_line):
484-
r"""Multiple imports in form from x import (a, b, c) should have
485-
space between import statement and parenthesised name list.
483+
def missing_whitespace_after_keyword(logical_line, tokens):
484+
r"""Keywords should be followed by whitespace.
486485
487486
Okay: from foo import (bar, baz)
488487
E275: from foo import(bar, baz)
489488
E275: from importable.module import(bar, baz)
489+
E275: if(foo): bar
490490
"""
491-
line = logical_line
492-
indicator = ' import('
493-
if line.startswith('from '):
494-
found = line.find(indicator)
495-
if -1 < found:
496-
pos = found + len(indicator) - 1
491+
for tok0, tok1 in zip(tokens, tokens[1:]):
492+
# This must exclude the True/False/None singletons, which can
493+
# appear e.g. as "if x is None:", and async/await, which were
494+
# valid identifier names in old Python versions.
495+
if (tok0.end == tok1.start and
496+
keyword.iskeyword(tok0.string) and
497+
tok0.string not in SINGLETONS and
498+
tok0.string not in ('async', 'await') and
499+
tok1.string not in ':\n'):
500+
line, pos = tok0.end
497501
yield pos, "E275 missing whitespace after keyword"
498502

499503

testsuite/E12not.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040

4141
if start[1] > end_col and not (
4242
over_indent == 4 and indent_next):
43-
return(0, "E121 continuation line over-"
44-
"indented for visual indent")
43+
return (0, "E121 continuation line over-"
44+
"indented for visual indent")
4545

4646

4747
print "OK", ("visual",
@@ -175,7 +175,7 @@ def long_function_name(
175175
#
176176

177177
if bar:
178-
return(
178+
return (
179179
start, 'E121 lines starting with a '
180180
'closing bracket should be indented '
181181
"to match that of the opening "

testsuite/E27.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@
4242
from importable.module import(e, f)
4343
except ImportError:
4444
pass
45+
#: E275
46+
if(foo):
47+
pass
48+
else:
49+
pass
4550
#: Okay
4651
matched = {"true": True, "false": False}

testsuite/W19.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
#: E101 E101 W191 W191
4242
if start[1] > end_col and not (
4343
over_indent == 4 and indent_next):
44-
return(0, "E121 continuation line over-"
45-
"indented for visual indent")
44+
return (0, "E121 continuation line over-"
45+
"indented for visual indent")
4646
#:
4747

4848
#: E101 W191
@@ -58,7 +58,7 @@ def long_function_name(
5858
raise Exception("%s,%s - %s" % (row, col, self.moduleCount))
5959
#: E101 E101 E101 E101 W191 W191 W191 W191 W191 W191
6060
if bar:
61-
return(
61+
return (
6262
start, 'E121 lines starting with a '
6363
'closing bracket should be indented '
6464
"to match that of the opening "

0 commit comments

Comments
 (0)