Skip to content

Commit 6b75c4d

Browse files
beneschIanLee1521
authored andcommitted
Fix issue #400: Require two blank lines after toplevel def, class
1 parent dc08dad commit 6b75c4d

File tree

8 files changed

+50
-1
lines changed

8 files changed

+50
-1
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Changes:
2626
* Added check E275 for whitespace on `from ... import ...` lines; #489 / #491
2727
* Added W503 to the list of codes ignored by default ignore list; #498
2828
* Removed use of project level `.pep8` configuration file; #364
29+
* Added check E305 for two blank lines after toplevel method and toplevel class; #400
2930

3031
Bugs:
3132

docs/intro.rst

+2
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ This is the current list of error and warning codes:
322322
+------------+----------------------------------------------------------------------+
323323
| E304 | blank lines found after function decorator |
324324
+------------+----------------------------------------------------------------------+
325+
| E305 | expected 2 blank lines after end of function or class |
326+
+------------+----------------------------------------------------------------------+
325327
+------------+----------------------------------------------------------------------+
326328
| **E4** | *Import* |
327329
+------------+----------------------------------------------------------------------+

pycodestyle.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ def maximum_line_length(physical_line, max_line_length, multiline, noqa):
237237

238238

239239
def blank_lines(logical_line, blank_lines, indent_level, line_number,
240-
blank_before, previous_logical, previous_indent_level):
240+
blank_before, previous_logical, previous_logical_toplevel,
241+
previous_indent_level):
241242
r"""Separate top-level function and class definitions with two blank lines.
242243
243244
Method definitions inside a class are separated by a single blank line.
@@ -256,6 +257,7 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
256257
E303: def a():\n pass\n\n\n\ndef b(n):\n pass
257258
E303: def a():\n\n\n\n pass
258259
E304: @decorator\n\ndef a():\n pass
260+
E305: def a():\n pass\na()
259261
"""
260262
if line_number < 3 and not previous_logical:
261263
return # Don't expect blank lines before the first line
@@ -271,6 +273,10 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
271273
yield 0, "E301 expected 1 blank line, found 0"
272274
elif blank_before != 2:
273275
yield 0, "E302 expected 2 blank lines, found %d" % blank_before
276+
elif (logical_line and not indent_level and blank_before != 2 and
277+
previous_logical_toplevel.startswith(('def', 'class'))):
278+
yield 0, "E305 expected 2 blank lines after " \
279+
"class or function definition, found %d" % blank_before
274280

275281

276282
def extraneous_whitespace(logical_line):
@@ -1450,6 +1456,8 @@ def init_checks_registry():
14501456
mod = inspect.getmodule(register_check)
14511457
for (name, function) in inspect.getmembers(mod, inspect.isfunction):
14521458
register_check(function)
1459+
1460+
14531461
init_checks_registry()
14541462

14551463

@@ -1608,6 +1616,8 @@ def check_logical(self):
16081616
if self.logical_line:
16091617
self.previous_indent_level = self.indent_level
16101618
self.previous_logical = self.logical_line
1619+
if not self.indent_level:
1620+
self.previous_logical_toplevel = self.logical_line
16111621
self.blank_lines = 0
16121622
self.tokens = []
16131623

@@ -1678,6 +1688,7 @@ def check_all(self, expected=None, line_offset=0):
16781688
self.indent_char = None
16791689
self.indent_level = self.previous_indent_level = 0
16801690
self.previous_logical = ''
1691+
self.previous_logical_toplevel = ''
16811692
self.tokens = []
16821693
self.blank_lines = self.blank_before = 0
16831694
parens = 0

testsuite/E12not.py

+3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def long_function_name(
139139
var_four):
140140
print(var_one)
141141

142+
142143
if ((row < 0 or self.moduleCount <= row or
143144
col < 0 or self.moduleCount <= col)):
144145
raise Exception("%s,%s - %s" % (row, col, self.moduleCount))
@@ -400,6 +401,7 @@ def unicode2html(s):
400401
.replace('"', '&#34;')
401402
.replace('\n', '<br>\n'))
402403

404+
403405
#
404406
parser.add_option('--count', action='store_true',
405407
help="print total number of errors and warnings "
@@ -616,6 +618,7 @@ def other_example():
616618
for key, val in node.items()
617619
))]
618620

621+
619622
foo([
620623
'bug'
621624
])

testsuite/E22.py

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def halves(n):
150150
def squares(n):
151151
return (i**2 for i in range(n))
152152

153+
153154
ENG_PREFIXES = {
154155
-6: "\u03bc", # Greek letter mu
155156
-3: "m",

testsuite/E30.py

+29
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+
#:

testsuite/support.py

+1
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,6 @@ def run_tests(style):
196196
init_tests(style)
197197
return style.check_files()
198198

199+
199200
# nose should not collect these functions
200201
init_tests.__test__ = run_tests.__test__ = False

testsuite/test_all.py

+1
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ def suite():
6262
def _main():
6363
return unittest.TextTestRunner(verbosity=2).run(suite())
6464

65+
6566
if __name__ == '__main__':
6667
sys.exit(not _main())

0 commit comments

Comments
 (0)