Skip to content

Commit 36e3400

Browse files
committed
Special case for nested functions an classes (fixes #28)
1 parent 4438622 commit 36e3400

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

pycodestyle.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ def maximum_line_length(physical_line, max_line_length, multiline, noqa):
238238

239239
def blank_lines(logical_line, blank_lines, indent_level, line_number,
240240
blank_before, previous_logical,
241-
previous_unindented_logical_line, previous_indent_level):
241+
previous_unindented_logical_line, previous_indent_level,
242+
lines):
242243
r"""Separate top-level function and class definitions with two blank lines.
243244
244245
Method definitions inside a class are separated by a single blank line.
@@ -270,7 +271,19 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
270271
if indent_level:
271272
if not (blank_before or previous_indent_level < indent_level or
272273
DOCSTRING_REGEX.match(previous_logical)):
273-
yield 0, "E301 expected 1 blank line, found 0"
274+
ancestor_level = indent_level
275+
nested = False
276+
for line in lines[line_number - 2::-1]:
277+
if line.strip() and expand_indent(line) < ancestor_level:
278+
ancestor_level = expand_indent(line)
279+
nested = line.lstrip().startswith('def ')
280+
if nested or ancestor_level == 0:
281+
break
282+
if nested:
283+
yield 0, "E306 expected 1 blank line, found 0" \
284+
" (nested definition)"
285+
else:
286+
yield 0, "E301 expected 1 blank line, found 0"
274287
elif blank_before != 2:
275288
yield 0, "E302 expected 2 blank lines, found %d" % blank_before
276289
elif (logical_line and not indent_level and blank_before != 2 and

testsuite/E30.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@ def a():
118118
a()
119119
#:
120120

121+
#: E306:3:5
122+
def a():
123+
x = 1
124+
def b():
125+
pass
126+
#: E306:3:5 E306:5:9
127+
def a():
128+
x = 2
129+
def b():
130+
x = 1
131+
def c():
132+
pass
133+
#: E306:3:5 E306:6:5
134+
def a():
135+
x = 1
136+
class C:
137+
pass
138+
x = 2
139+
def b():
140+
pass
141+
#:
142+
121143
#: E305:8:1
122144
# Example from https://github.com/PyCQA/pycodestyle/issues/400
123145
import stuff

0 commit comments

Comments
 (0)