Skip to content

Commit

Permalink
Require two blank lines after toplevel def/class; issue PyCQA#400
Browse files Browse the repository at this point in the history
  • Loading branch information
benesch committed May 20, 2015
1 parent 435d1cb commit 9693205
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ def maximum_line_length(physical_line, max_line_length, multiline):


def blank_lines(logical_line, blank_lines, indent_level, line_number,
blank_before, previous_logical, previous_indent_level):
blank_before, previous_logical, previous_logical_toplevel,
previous_indent_level):
r"""Separate top-level function and class definitions with two blank lines.
Method definitions inside a class are separated by a single blank line.
Expand All @@ -253,6 +254,7 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
E303: def a():\n pass\n\n\n\ndef b(n):\n pass
E303: def a():\n\n\n\n pass
E304: @decorator\n\ndef a():\n pass
E305: def a():\n pass\na()
"""
if line_number < 3 and not previous_logical:
return # Don't expect blank lines before the first line
Expand All @@ -268,6 +270,9 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
yield 0, "E301 expected 1 blank line, found 0"
elif blank_before != 2:
yield 0, "E302 expected 2 blank lines, found %d" % blank_before
elif (logical_line and not indent_level and blank_before != 2 and
previous_logical_toplevel.startswith(('def', 'class'))):
yield 0, "E305 expected 2 blank lines before, found %d" % blank_before


def extraneous_whitespace(logical_line):
Expand Down Expand Up @@ -1497,6 +1502,8 @@ def check_logical(self):
if self.logical_line:
self.previous_indent_level = self.indent_level
self.previous_logical = self.logical_line
if not self.indent_level:
self.previous_logical_toplevel = self.logical_line
self.blank_lines = 0
self.tokens = []

Expand Down Expand Up @@ -1566,6 +1573,7 @@ def check_all(self, expected=None, line_offset=0):
self.indent_char = None
self.indent_level = self.previous_indent_level = 0
self.previous_logical = ''
self.previous_logical_toplevel = ''
self.tokens = []
self.blank_lines = self.blank_before = 0
parens = 0
Expand Down
29 changes: 29 additions & 0 deletions testsuite/E30.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,32 @@ def function():
It gives error E303: too many blank lines (3)
"""
#:

#: E305:7:1
def a():
print

# comment

# another comment
a()
#: E305:8:1
def a():
print

# comment

# another comment

try:
a()
except:
pass
#: E305:5:1
def a():
print

# Two spaces before comments, too.
if a():
a()
#:

0 comments on commit 9693205

Please sign in to comment.