@@ -95,6 +95,13 @@ def lru_cache(maxsize=128): # noqa as it's a fake implementation.
9595PROJECT_CONFIG = ('setup.cfg' , 'tox.ini' )
9696TESTSUITE_PATH = os .path .join (os .path .dirname (__file__ ), 'testsuite' )
9797MAX_LINE_LENGTH = 79
98+ # Number of blank lines between various code parts.
99+ BLANK_LINES_CONFIG = {
100+ # Top level class and function.
101+ 'top_level' : 2 ,
102+ # Methods and nested class and function.
103+ 'method' : 1 ,
104+ }
98105REPORT_FORMAT = {
99106 'default' : '%(path)s:%(row)d:%(col)d: %(code)s %(text)s' ,
100107 'pylint' : '%(path)s:%(row)d: [%(code)s] %(text)s' ,
@@ -332,37 +339,50 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
332339 E305: def a():\n pass\na()
333340 E306: def a():\n def b():\n pass\n def c():\n pass
334341 """
335- if line_number < 3 and not previous_logical :
342+ top_level_lines = BLANK_LINES_CONFIG ['top_level' ]
343+ method_lines = BLANK_LINES_CONFIG ['method' ]
344+
345+ if line_number < top_level_lines + 1 and not previous_logical :
336346 return # Don't expect blank lines before the first line
337347 if previous_logical .startswith ('@' ):
338348 if blank_lines :
339349 yield 0 , "E304 blank lines found after function decorator"
340- elif blank_lines > 2 or (indent_level and blank_lines == 2 ):
350+ elif (blank_lines > top_level_lines or
351+ (indent_level and blank_lines == method_lines + 1 )
352+ ):
341353 yield 0 , "E303 too many blank lines (%d)" % blank_lines
342354 elif STARTSWITH_TOP_LEVEL_REGEX .match (logical_line ):
343355 if indent_level :
344- if not (blank_before or previous_indent_level < indent_level or
345- DOCSTRING_REGEX .match (previous_logical )):
356+ if not (blank_before == method_lines or
357+ previous_indent_level < indent_level or
358+ DOCSTRING_REGEX .match (previous_logical )
359+ ):
346360 ancestor_level = indent_level
347361 nested = False
348362 # Search backwards for a def ancestor or tree root (top level).
349- for line in lines [line_number - 2 ::- 1 ]:
363+ for line in lines [line_number - top_level_lines ::- 1 ]:
350364 if line .strip () and expand_indent (line ) < ancestor_level :
351365 ancestor_level = expand_indent (line )
352366 nested = line .lstrip ().startswith ('def ' )
353367 if nested or ancestor_level == 0 :
354368 break
355369 if nested :
356- yield 0 , "E306 expected 1 blank line before a " \
357- "nested definition, found 0"
370+ yield 0 , "E306 expected %s blank line before a " \
371+ "nested definition, found 0" % ( method_lines ,)
358372 else :
359- yield 0 , "E301 expected 1 blank line, found 0"
360- elif blank_before != 2 :
361- yield 0 , "E302 expected 2 blank lines, found %d" % blank_before
362- elif (logical_line and not indent_level and blank_before != 2 and
363- previous_unindented_logical_line .startswith (('def ' , 'class ' ))):
364- yield 0 , "E305 expected 2 blank lines after " \
365- "class or function definition, found %d" % blank_before
373+ yield 0 , "E301 expected %s blank line, found 0" % (
374+ method_lines ,)
375+ elif blank_before != top_level_lines :
376+ yield 0 , "E302 expected %s blank lines, found %d" % (
377+ top_level_lines , blank_before )
378+ elif (logical_line and
379+ not indent_level and
380+ blank_before != top_level_lines and
381+ previous_unindented_logical_line .startswith (('def ' , 'class ' ))
382+ ):
383+ yield 0 , "E305 expected %s blank lines after " \
384+ "class or function definition, found %d" % (
385+ top_level_lines , blank_before )
366386
367387
368388@register_check
0 commit comments