From 30c7c4822db6db1a68672b1d6c6d951eacae4ae6 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 24 Jan 2017 05:07:29 -0800 Subject: Report E704 for async def as well (#611) Fix detection of 'async def' (with multiple spaces) so that it also reports E704 --- pycodestyle.py | 8 +++++--- testsuite/E25.py | 4 ++++ testsuite/E30.py | 7 +++++++ testsuite/E70.py | 4 ++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pycodestyle.py b/pycodestyle.py index 559048e..0f14209 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -121,6 +121,8 @@ 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|@)') # Work around Python < 2.6 behaviour, which does not generate NL after # a comment which is on a line by itself. @@ -272,7 +274,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)): @@ -813,7 +815,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: @@ -1000,7 +1002,7 @@ 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: yield found, "E701 multiple statements on one line (colon)" 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 -- cgit v1.2.1