summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormemeplex <carlosjosepita@gmail.com>2016-06-28 21:16:36 -0300
committermemeplex <carlosjosepita@gmail.com>2016-06-28 21:16:36 -0300
commit36e3400f65268d1ee76845a74aae5b0112182ef8 (patch)
treefce1917ce6a2adeb6663feeb4e9157d152318a25
parent4438622d0b62df53a1999301d1bdc9fa119ae763 (diff)
downloadpep8-36e3400f65268d1ee76845a74aae5b0112182ef8.tar.gz
Special case for nested functions an classes (fixes #28)
-rwxr-xr-xpycodestyle.py17
-rw-r--r--testsuite/E30.py22
2 files changed, 37 insertions, 2 deletions
diff --git a/pycodestyle.py b/pycodestyle.py
index a5f5534..a043f04 100755
--- a/pycodestyle.py
+++ b/pycodestyle.py
@@ -238,7 +238,8 @@ def maximum_line_length(physical_line, max_line_length, multiline, noqa):
def blank_lines(logical_line, blank_lines, indent_level, line_number,
blank_before, previous_logical,
- previous_unindented_logical_line, previous_indent_level):
+ previous_unindented_logical_line, previous_indent_level,
+ lines):
r"""Separate top-level function and class definitions with two blank lines.
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,
if indent_level:
if not (blank_before or previous_indent_level < indent_level or
DOCSTRING_REGEX.match(previous_logical)):
- yield 0, "E301 expected 1 blank line, found 0"
+ ancestor_level = indent_level
+ nested = False
+ for line in lines[line_number - 2::-1]:
+ if line.strip() and expand_indent(line) < ancestor_level:
+ ancestor_level = expand_indent(line)
+ nested = line.lstrip().startswith('def ')
+ if nested or ancestor_level == 0:
+ break
+ if nested:
+ yield 0, "E306 expected 1 blank line, found 0" \
+ " (nested definition)"
+ else:
+ 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
diff --git a/testsuite/E30.py b/testsuite/E30.py
index 1471079..e5a3504 100644
--- a/testsuite/E30.py
+++ b/testsuite/E30.py
@@ -118,6 +118,28 @@ if a():
a()
#:
+#: E306:3:5
+def a():
+ x = 1
+ def b():
+ pass
+#: E306:3:5 E306:5:9
+def a():
+ x = 2
+ def b():
+ x = 1
+ def c():
+ pass
+#: E306:3:5 E306:6:5
+def a():
+ x = 1
+ class C:
+ pass
+ x = 2
+ def b():
+ pass
+#:
+
#: E305:8:1
# Example from https://github.com/PyCQA/pycodestyle/issues/400
import stuff