summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Sottile <asottile@umich.edu>2022-04-17 11:20:13 -0400
committerGitHub <noreply@github.com>2022-04-17 11:20:13 -0400
commit77972c4b9bc5603775718a62b6480fc1004f92a4 (patch)
treeb177b33b4d4788f6384c8ec23c222cc8d992b839
parentc9bb97f9f80fec09711189a02a82efeffdcd01f9 (diff)
parent52513b30195fe0d985a64e972c1218489cae7042 (diff)
downloadpep8-77972c4b9bc5603775718a62b6480fc1004f92a4.tar.gz
Merge pull request #1063 from anntzer/e275+
Generalize E275 to require space after all keywords, not just "import".
-rwxr-xr-xpycodestyle.py22
-rw-r--r--testsuite/E12not.py6
-rw-r--r--testsuite/E27.py5
-rw-r--r--testsuite/W19.py6
4 files changed, 24 insertions, 15 deletions
diff --git a/pycodestyle.py b/pycodestyle.py
index 2cbc2cd..e2e4b96 100755
--- a/pycodestyle.py
+++ b/pycodestyle.py
@@ -480,20 +480,24 @@ 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
"""
- 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"
diff --git a/testsuite/E12not.py b/testsuite/E12not.py
index 2e2366c..7677659 100644
--- a/testsuite/E12not.py
+++ b/testsuite/E12not.py
@@ -40,8 +40,8 @@ a = (123,
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",
@@ -175,7 +175,7 @@ fooff(aaaa,
#
if bar:
- return(
+ return (
start, 'E121 lines starting with a '
'closing bracket should be indented '
"to match that of the opening "
diff --git a/testsuite/E27.py b/testsuite/E27.py
index 9bb53f8..91aa079 100644
--- a/testsuite/E27.py
+++ b/testsuite/E27.py
@@ -42,5 +42,10 @@ try:
from importable.module import(e, f)
except ImportError:
pass
+#: E275
+if(foo):
+ pass
+else:
+ pass
#: Okay
matched = {"true": True, "false": False}
diff --git a/testsuite/W19.py b/testsuite/W19.py
index ed69e2b..9d4eb4d 100644
--- a/testsuite/W19.py
+++ b/testsuite/W19.py
@@ -41,8 +41,8 @@ if (
#: 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
@@ -58,7 +58,7 @@ if ((row < 0 or self.moduleCount <= row or
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 "