Skip to content

Commit 08f4891

Browse files
committed
Merge branch 'gh-536'
Closes #400, Closes #402, Closes #536
2 parents dc08dad + a0d5d5d commit 08f4891

File tree

9 files changed

+80
-1
lines changed

9 files changed

+80
-1
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Changes:
1010
* Improved performance of `compound_statements` check; #314 / #522
1111
* Fixed remaining references to `pep8`; #518 / #530
1212
* Added `noqa` support for `maximum_line_length` check; #538
13+
* Added check E305 for two blank lines after toplevel methods and classes; #400
1314

1415

1516
2.0.0 (2016-05-31)

docs/intro.rst

Lines changed: 2 additions & 0 deletions
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

Lines changed: 12 additions & 1 deletion
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,
241+
previous_unindented_logical_line, 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_unindented_logical_line.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_unindented_logical_line = 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_unindented_logical_line = ''
16811692
self.tokens = []
16821693
self.blank_lines = self.blank_before = 0
16831694
parens = 0

testsuite/E12not.py

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,43 @@ 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+
#:
120+
121+
#: E305:8:1
122+
# Example from https://github.com/PyCQA/pycodestyle/issues/400
123+
import stuff
124+
125+
126+
def main():
127+
blah, blah
128+
129+
if __name__ == '__main__':
130+
main()

testsuite/E30not.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,22 @@ def a():
132132
def b():
133133

134134
pass
135+
#: Okay
136+
def foo():
137+
pass
138+
139+
140+
def bar():
141+
pass
142+
143+
144+
class Foo(object):
145+
pass
146+
147+
148+
class Bar(object):
149+
pass
150+
151+
152+
if __name__ == '__main__':
153+
foo()

testsuite/support.py

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)