Skip to content

Commit

Permalink
Generalize E275 to require space after all keywords, not just "import".
Browse files Browse the repository at this point in the history
  • Loading branch information
anntzer committed Apr 16, 2022
1 parent c9bb97f commit 9f33905
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
23 changes: 14 additions & 9 deletions pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,20 +480,25 @@ def whitespace_around_keywords(logical_line):


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


Expand Down
6 changes: 3 additions & 3 deletions testsuite/E12not.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@

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


print "OK", ("visual",
Expand Down Expand Up @@ -175,7 +175,7 @@ def long_function_name(
#

if bar:
return(
return (
start, 'E121 lines starting with a '
'closing bracket should be indented '
"to match that of the opening "
Expand Down
7 changes: 7 additions & 0 deletions testsuite/E27.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,12 @@
from importable.module import(e, f)
except ImportError:
pass
#: E275
if(foo):
pass
else:
pass
#: E275
1 and(2 or 3)
#: Okay
matched = {"true": True, "false": False}
6 changes: 3 additions & 3 deletions testsuite/W19.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
#: E101 E101 W191 W191
if start[1] > end_col and not (
over_indent == 4 and indent_next):
return(0, "E121 continuation line over-"
"indented for visual indent")
return (0, "E121 continuation line over-"
"indented for visual indent")
#:

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

0 comments on commit 9f33905

Please sign in to comment.