summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xpycodestyle.py21
-rw-r--r--testsuite/E25.py4
-rw-r--r--testsuite/E30.py7
-rw-r--r--testsuite/E70.py4
-rw-r--r--testsuite/E90.py2
-rw-r--r--testsuite/python3.py12
6 files changed, 45 insertions, 5 deletions
diff --git a/pycodestyle.py b/pycodestyle.py
index edeb494..e0df43f 100755
--- a/pycodestyle.py
+++ b/pycodestyle.py
@@ -121,6 +121,19 @@ KEYWORD_REGEX = re.compile(r'(\s*)\b(?:%s)\b(\s*)' % r'|'.join(KEYWORDS))
OPERATOR_REGEX = re.compile(r'(?:[^,\s])(\s*)(?:[-+*/|!<=>%&^]+)(\s*)')
LAMBDA_REGEX = re.compile(r'\blambda\b')
HUNK_REGEX = re.compile(r'^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@.*$')
+STARTSWITH_DEF_REGEX = re.compile(r'^(async\s+def|def)')
+STARTSWITH_TOP_LEVEL_REGEX = re.compile(r'^(async\s+def|def|class|@)')
+STARTSWITH_INDENT_STATEMENT_REGEX = re.compile(
+ r'^\s*({0})'.format('|'.join(s.replace(' ', '\s+') for s in (
+ 'def', 'async def',
+ 'for', 'async for',
+ 'if', 'elif', 'else',
+ 'try', 'except', 'finally',
+ 'with', 'async with',
+ 'class',
+ 'while',
+ )))
+)
# Work around Python < 2.6 behaviour, which does not generate NL after
# a comment which is on a line by itself.
@@ -273,7 +286,7 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
yield 0, "E304 blank lines found after function decorator"
elif blank_lines > 2 or (indent_level and blank_lines == 2):
yield 0, "E303 too many blank lines (%d)" % blank_lines
- elif logical_line.startswith(('def ', 'async def', 'class ', '@')):
+ elif STARTSWITH_TOP_LEVEL_REGEX.match(logical_line):
if indent_level:
if not (blank_before or previous_indent_level < indent_level or
DOCSTRING_REGEX.match(previous_logical)):
@@ -814,7 +827,7 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
no_space = False
prev_end = None
annotated_func_arg = False
- in_def = logical_line.startswith(('def', 'async def'))
+ in_def = bool(STARTSWITH_DEF_REGEX.match(logical_line))
message = "E251 unexpected spaces around keyword / parameter equals"
for token_type, text, start, end, line in tokens:
if token_type == tokenize.NL:
@@ -1001,9 +1014,9 @@ def compound_statements(logical_line):
yield 0, ("E731 do not assign a lambda expression, use a "
"def")
break
- if line.startswith('def '):
+ if STARTSWITH_DEF_REGEX.match(line):
yield 0, "E704 multiple statements on one line (def)"
- else:
+ elif STARTSWITH_INDENT_STATEMENT_REGEX.match(line):
yield found, "E701 multiple statements on one line (colon)"
prev_found = found
found = line.find(':', found + 1)
diff --git a/testsuite/E25.py b/testsuite/E25.py
index 7a536b5..dde95b8 100644
--- a/testsuite/E25.py
+++ b/testsuite/E25.py
@@ -38,3 +38,7 @@ def munge(input: AnyStr, sep: AnyStr = None, limit=1000,
#: Okay
async def add(a: int = 0, b: int = 0) -> int:
return a + b
+# Previously E251 four times
+#: E272:1:6
+async def add(a: int = 0, b: int = 0) -> int:
+ return a + b
diff --git a/testsuite/E30.py b/testsuite/E30.py
index aaa77b7..bd74b80 100644
--- a/testsuite/E30.py
+++ b/testsuite/E30.py
@@ -156,3 +156,10 @@ def main():
if __name__ == '__main__':
main()
+# Previously just E272:1:6 E272:4:6
+#: E302:4:1 E272:1:6 E272:4:6
+async def x():
+ pass
+
+async def x(y: int = 1):
+ pass
diff --git a/testsuite/E70.py b/testsuite/E70.py
index 85ca666..caafe45 100644
--- a/testsuite/E70.py
+++ b/testsuite/E70.py
@@ -12,6 +12,10 @@ import shlex;
del a[:]; a.append(42);
#: E704:1:1
def f(x): return 2
+#: E704:1:1
+async def f(x): return 2
+#: E704:1:1 E272:1:6
+async def f(x): return 2
#: E704:1:1 E226:1:19
def f(x): return 2*x
#: E704:2:5 E226:2:23
diff --git a/testsuite/E90.py b/testsuite/E90.py
index 1db3d0e..2c18e9a 100644
--- a/testsuite/E90.py
+++ b/testsuite/E90.py
@@ -8,7 +8,7 @@ while True:
pass
except:
print 'Whoops'
-#: E122 E225 E251 E251 E701
+#: E122 E225 E251 E251
# Do not crash if code is invalid
if msg:
diff --git a/testsuite/python3.py b/testsuite/python3.py
index 8881880..fde4281 100644
--- a/testsuite/python3.py
+++ b/testsuite/python3.py
@@ -1,6 +1,18 @@
#!/usr/bin/env python3
+from typing import ClassVar, List
# Annotated function (Issue #29)
def foo(x: int) -> int:
return x + 1
+
+
+# Annotated variables #575
+CONST: int = 42
+
+
+class Class:
+ cls_var: ClassVar[str]
+
+ def m(self):
+ xs: List[int] = []