|
121 | 121 | OPERATOR_REGEX = re.compile(r'(?:[^,\s])(\s*)(?:[-+*/|!<=>%&^]+)(\s*)')
|
122 | 122 | LAMBDA_REGEX = re.compile(r'\blambda\b')
|
123 | 123 | 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|@)') |
124 | 126 |
|
125 | 127 | # Work around Python < 2.6 behaviour, which does not generate NL after
|
126 | 128 | # a comment which is on a line by itself.
|
@@ -272,7 +274,7 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
|
272 | 274 | yield 0, "E304 blank lines found after function decorator"
|
273 | 275 | elif blank_lines > 2 or (indent_level and blank_lines == 2):
|
274 | 276 | 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): |
276 | 278 | if indent_level:
|
277 | 279 | if not (blank_before or previous_indent_level < indent_level or
|
278 | 280 | DOCSTRING_REGEX.match(previous_logical)):
|
@@ -813,7 +815,7 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
|
813 | 815 | no_space = False
|
814 | 816 | prev_end = None
|
815 | 817 | annotated_func_arg = False
|
816 |
| - in_def = logical_line.startswith(('def', 'async def')) |
| 818 | + in_def = bool(STARTSWITH_DEF_REGEX.match(logical_line)) |
817 | 819 | message = "E251 unexpected spaces around keyword / parameter equals"
|
818 | 820 | for token_type, text, start, end, line in tokens:
|
819 | 821 | if token_type == tokenize.NL:
|
@@ -1000,7 +1002,7 @@ def compound_statements(logical_line):
|
1000 | 1002 | yield 0, ("E731 do not assign a lambda expression, use a "
|
1001 | 1003 | "def")
|
1002 | 1004 | break
|
1003 |
| - if line.startswith('def '): |
| 1005 | + if STARTSWITH_DEF_REGEX.match(line): |
1004 | 1006 | yield 0, "E704 multiple statements on one line (def)"
|
1005 | 1007 | else:
|
1006 | 1008 | yield found, "E701 multiple statements on one line (colon)"
|
|
0 commit comments