@@ -314,6 +314,41 @@ def maximum_line_length(physical_line, max_line_length, multiline,
314
314
########################################################################
315
315
316
316
317
+ def _is_one_liner (logical_line , indent_level , lines , line_number ):
318
+ if not STARTSWITH_TOP_LEVEL_REGEX .match (logical_line ):
319
+ return False
320
+
321
+ line_idx = line_number - 1
322
+
323
+ if line_idx < 1 :
324
+ prev_indent = 0
325
+ else :
326
+ prev_indent = expand_indent (lines [line_idx - 1 ])
327
+
328
+ if prev_indent > indent_level :
329
+ return False
330
+
331
+ while line_idx < len (lines ):
332
+ line = lines [line_idx ].strip ()
333
+ if not line .startswith ('@' ) and STARTSWITH_TOP_LEVEL_REGEX .match (line ):
334
+ break
335
+ else :
336
+ line_idx += 1
337
+ else :
338
+ return False # invalid syntax: EOF while searching for def/class
339
+
340
+ next_idx = line_idx + 1
341
+ while next_idx < len (lines ):
342
+ if lines [next_idx ].strip ():
343
+ break
344
+ else :
345
+ next_idx += 1
346
+ else :
347
+ return True # line is last in the file
348
+
349
+ return expand_indent (lines [next_idx ]) <= indent_level
350
+
351
+
317
352
@register_check
318
353
def blank_lines (logical_line , blank_lines , indent_level , line_number ,
319
354
blank_before , previous_logical ,
@@ -360,16 +395,11 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
360
395
):
361
396
yield 0 , "E303 too many blank lines (%d)" % blank_lines
362
397
elif STARTSWITH_TOP_LEVEL_REGEX .match (logical_line ):
363
- # If this is a one-liner (i.e. this is not a decorator and the
364
- # next line is not more indented), and the previous line is also
365
- # not deeper (it would be better to check if the previous line
366
- # is part of another def/class at the same level), don't require
367
- # blank lines around this.
368
- prev_line = lines [line_number - 2 ] if line_number >= 2 else ''
369
- next_line = lines [line_number ] if line_number < len (lines ) else ''
370
- if (not logical_line .startswith ("@" ) and
371
- expand_indent (prev_line ) <= indent_level and
372
- expand_indent (next_line ) <= indent_level ):
398
+ # allow a group of one-liners
399
+ if (
400
+ _is_one_liner (logical_line , indent_level , lines , line_number ) and
401
+ blank_before == 0
402
+ ):
373
403
return
374
404
if indent_level :
375
405
if not (blank_before == method_lines or
0 commit comments