Skip to content

Commit 30c7c48

Browse files
asottilesigmavirus24
authored andcommitted
Report E704 for async def as well (#611)
Fix detection of 'async def' (with multiple spaces) so that it also reports E704
1 parent d7d6154 commit 30c7c48

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

pycodestyle.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@
121121
OPERATOR_REGEX = re.compile(r'(?:[^,\s])(\s*)(?:[-+*/|!<=>%&^]+)(\s*)')
122122
LAMBDA_REGEX = re.compile(r'\blambda\b')
123123
HUNK_REGEX = re.compile(r'^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@.*$')
124+
STARTSWITH_DEF_REGEX = re.compile(r'^(async\s+def|def)')
125+
STARTSWITH_TOP_LEVEL_REGEX = re.compile(r'^(async\s+def|def|class|@)')
124126

125127
# Work around Python < 2.6 behaviour, which does not generate NL after
126128
# a comment which is on a line by itself.
@@ -272,7 +274,7 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
272274
yield 0, "E304 blank lines found after function decorator"
273275
elif blank_lines > 2 or (indent_level and blank_lines == 2):
274276
yield 0, "E303 too many blank lines (%d)" % blank_lines
275-
elif logical_line.startswith(('def ', 'async def', 'class ', '@')):
277+
elif STARTSWITH_TOP_LEVEL_REGEX.match(logical_line):
276278
if indent_level:
277279
if not (blank_before or previous_indent_level < indent_level or
278280
DOCSTRING_REGEX.match(previous_logical)):
@@ -813,7 +815,7 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
813815
no_space = False
814816
prev_end = None
815817
annotated_func_arg = False
816-
in_def = logical_line.startswith(('def', 'async def'))
818+
in_def = bool(STARTSWITH_DEF_REGEX.match(logical_line))
817819
message = "E251 unexpected spaces around keyword / parameter equals"
818820
for token_type, text, start, end, line in tokens:
819821
if token_type == tokenize.NL:
@@ -1000,7 +1002,7 @@ def compound_statements(logical_line):
10001002
yield 0, ("E731 do not assign a lambda expression, use a "
10011003
"def")
10021004
break
1003-
if line.startswith('def '):
1005+
if STARTSWITH_DEF_REGEX.match(line):
10041006
yield 0, "E704 multiple statements on one line (def)"
10051007
else:
10061008
yield found, "E701 multiple statements on one line (colon)"

testsuite/E25.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ def munge(input: AnyStr, sep: AnyStr = None, limit=1000,
3838
#: Okay
3939
async def add(a: int = 0, b: int = 0) -> int:
4040
return a + b
41+
# Previously E251 four times
42+
#: E272:1:6
43+
async def add(a: int = 0, b: int = 0) -> int:
44+
return a + b

testsuite/E30.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,10 @@ def main():
156156

157157
if __name__ == '__main__':
158158
main()
159+
# Previously just E272:1:6 E272:4:6
160+
#: E302:4:1 E272:1:6 E272:4:6
161+
async def x():
162+
pass
163+
164+
async def x(y: int = 1):
165+
pass

testsuite/E70.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
del a[:]; a.append(42);
1313
#: E704:1:1
1414
def f(x): return 2
15+
#: E704:1:1
16+
async def f(x): return 2
17+
#: E704:1:1 E272:1:6
18+
async def f(x): return 2
1519
#: E704:1:1 E226:1:19
1620
def f(x): return 2*x
1721
#: E704:2:5 E226:2:23

0 commit comments

Comments
 (0)