Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -270,7 +271,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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to store the result of expand_indent(line) since you use it again in the next line?

Could be the same with line.strip() which is reused as line.lstrip() in L280.

@memeplex memeplex Jul 1, 2016

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to do it that way because it keeps the code shorter and the second evaluation is not very frequent.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, it's a minor nit anyways. :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding another approach please read my last comments at the conversation tab.

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
Expand Down
22 changes: 22 additions & 0 deletions testsuite/E30.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ def a():
a()
#:

#: E306:3:5

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me, but could you maybe add some #: Okay test cases that verify the desired behavior?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea @IanLee1521

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. I'm not sure if okay tests apply here, because I just divided a set of already existent error types in two, so non-errors are still the same.

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
#:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't realize this was to prevent the test from being confused.


#: E305:8:1
# Example from https://github.com/PyCQA/pycodestyle/issues/400
import stuff
Expand Down