summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Cordasco <sigmavirus24@users.noreply.github.com>2016-08-11 06:17:01 -0500
committerGitHub <noreply@github.com>2016-08-11 06:17:01 -0500
commit39e57186620c8d8c87eaf4b0f3b00ff90b716f7e (patch)
tree084f0c08ff71ca1fc4e14f54224d3d008935d62c
parent66e43b4fea8c5b99c1bb146b7bdc0b6d3980854c (diff)
parent98dee57c033626e34fe96bef7f86e48dbb71c121 (diff)
downloadpep8-39e57186620c8d8c87eaf4b0f3b00ff90b716f7e.tar.gz
Merge pull request #555 from memeplex/master
Special case for nested functions and classes (fixes #28)
-rwxr-xr-xpycodestyle.py18
-rw-r--r--testsuite/E30.py22
2 files changed, 38 insertions, 2 deletions
diff --git a/pycodestyle.py b/pycodestyle.py
index ea367f7..a14369e 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.
@@ -272,7 +273,20 @@ 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
+ # Search backwards for a def ancestor or tree root (top level).
+ 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 before a " \
+ "nested definition, found 0"
+ 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 9397541..105d368 100644
--- a/testsuite/E30.py
+++ b/testsuite/E30.py
@@ -124,6 +124,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