-
Notifications
You must be signed in to change notification settings - Fork 753
Fix Issue 400: Require two blank lines after toplevel def, class #536
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -237,7 +237,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_indent_level): | ||
blank_before, previous_logical, previous_logical_toplevel, | ||
previous_indent_level): | ||
r"""Separate top-level function and class definitions with two blank lines. | ||
|
||
Method definitions inside a class are separated by a single blank line. | ||
|
@@ -256,6 +257,7 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number, | |
E303: def a():\n pass\n\n\n\ndef b(n):\n pass | ||
E303: def a():\n\n\n\n pass | ||
E304: @decorator\n\ndef a():\n pass | ||
E305: def a():\n pass\na() | ||
""" | ||
if line_number < 3 and not previous_logical: | ||
return # Don't expect blank lines before the first line | ||
|
@@ -271,6 +273,10 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number, | |
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 | ||
previous_logical_toplevel.startswith(('def', 'class'))): | ||
yield 0, "E305 expected 2 blank lines after " \ | ||
"class or function definition, found %d" % blank_before | ||
|
||
|
||
def extraneous_whitespace(logical_line): | ||
|
@@ -1450,6 +1456,8 @@ def init_checks_registry(): | |
mod = inspect.getmodule(register_check) | ||
for (name, function) in inspect.getmembers(mod, inspect.isfunction): | ||
register_check(function) | ||
|
||
|
||
init_checks_registry() | ||
|
||
|
||
|
@@ -1608,6 +1616,8 @@ def check_logical(self): | |
if self.logical_line: | ||
self.previous_indent_level = self.indent_level | ||
self.previous_logical = self.logical_line | ||
if not self.indent_level: | ||
self.previous_logical_toplevel = self.logical_line | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This name is a bit poor. We're also now adding a new attribute to a Checker that a plugin can request. Given that we're exposing this to plugins, can we consider a better name? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've thought about it, I'm not sure what alternative variable name to use. Open to suggestions! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps I'm weird, but I think |
||
self.blank_lines = 0 | ||
self.tokens = [] | ||
|
||
|
@@ -1678,6 +1688,7 @@ def check_all(self, expected=None, line_offset=0): | |
self.indent_char = None | ||
self.indent_level = self.previous_indent_level = 0 | ||
self.previous_logical = '' | ||
self.previous_logical_toplevel = '' | ||
self.tokens = [] | ||
self.blank_lines = self.blank_before = 0 | ||
parens = 0 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should actually be up in the unreleased next version changes section.