From 36e3400f65268d1ee76845a74aae5b0112182ef8 Mon Sep 17 00:00:00 2001 From: memeplex Date: Tue, 28 Jun 2016 21:16:36 -0300 Subject: Special case for nested functions an classes (fixes #28) --- pycodestyle.py | 17 +++++++++++++++-- testsuite/E30.py | 22 ++++++++++++++++++++++ 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 -- cgit v1.2.1