Skip to content

Commit 9693205

Browse files
committed
Require two blank lines after toplevel def/class; issue PyCQA#400
1 parent 435d1cb commit 9693205

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

pep8.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ def maximum_line_length(physical_line, max_line_length, multiline):
234234

235235

236236
def blank_lines(logical_line, blank_lines, indent_level, line_number,
237-
blank_before, previous_logical, previous_indent_level):
237+
blank_before, previous_logical, previous_logical_toplevel,
238+
previous_indent_level):
238239
r"""Separate top-level function and class definitions with two blank lines.
239240
240241
Method definitions inside a class are separated by a single blank line.
@@ -253,6 +254,7 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
253254
E303: def a():\n pass\n\n\n\ndef b(n):\n pass
254255
E303: def a():\n\n\n\n pass
255256
E304: @decorator\n\ndef a():\n pass
257+
E305: def a():\n pass\na()
256258
"""
257259
if line_number < 3 and not previous_logical:
258260
return # Don't expect blank lines before the first line
@@ -268,6 +270,9 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
268270
yield 0, "E301 expected 1 blank line, found 0"
269271
elif blank_before != 2:
270272
yield 0, "E302 expected 2 blank lines, found %d" % blank_before
273+
elif (logical_line and not indent_level and blank_before != 2 and
274+
previous_logical_toplevel.startswith(('def', 'class'))):
275+
yield 0, "E305 expected 2 blank lines before, found %d" % blank_before
271276

272277

273278
def extraneous_whitespace(logical_line):
@@ -1497,6 +1502,8 @@ def check_logical(self):
14971502
if self.logical_line:
14981503
self.previous_indent_level = self.indent_level
14991504
self.previous_logical = self.logical_line
1505+
if not self.indent_level:
1506+
self.previous_logical_toplevel = self.logical_line
15001507
self.blank_lines = 0
15011508
self.tokens = []
15021509

@@ -1566,6 +1573,7 @@ def check_all(self, expected=None, line_offset=0):
15661573
self.indent_char = None
15671574
self.indent_level = self.previous_indent_level = 0
15681575
self.previous_logical = ''
1576+
self.previous_logical_toplevel = ''
15691577
self.tokens = []
15701578
self.blank_lines = self.blank_before = 0
15711579
parens = 0

testsuite/E30.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,32 @@ def function():
8888
It gives error E303: too many blank lines (3)
8989
"""
9090
#:
91+
92+
#: E305:7:1
93+
def a():
94+
print
95+
96+
# comment
97+
98+
# another comment
99+
a()
100+
#: E305:8:1
101+
def a():
102+
print
103+
104+
# comment
105+
106+
# another comment
107+
108+
try:
109+
a()
110+
except:
111+
pass
112+
#: E305:5:1
113+
def a():
114+
print
115+
116+
# Two spaces before comments, too.
117+
if a():
118+
a()
119+
#:

0 commit comments

Comments
 (0)