summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Lee <IanLee1521@gmail.com>2016-06-25 14:41:24 -0700
committerIan Lee <IanLee1521@gmail.com>2016-06-25 14:41:24 -0700
commit08f48912d88e4c8104813cc2834e634841f0301c (patch)
treec03aefebfac889324215af5a23b1aaf7ea69e963
parentdc08dadd84369185f3c19782fd727dbf5029a056 (diff)
parenta0d5d5d0bb2fc5e3cd75439298abce1f14323f1b (diff)
downloadpep8-08f48912d88e4c8104813cc2834e634841f0301c.tar.gz
Merge branch 'gh-536'
Closes #400, Closes #402, Closes #536
-rw-r--r--CHANGES.txt1
-rw-r--r--docs/intro.rst2
-rwxr-xr-xpycodestyle.py13
-rw-r--r--testsuite/E12not.py3
-rw-r--r--testsuite/E22.py1
-rw-r--r--testsuite/E30.py40
-rw-r--r--testsuite/E30not.py19
-rw-r--r--testsuite/support.py1
-rw-r--r--testsuite/test_all.py1
9 files changed, 80 insertions, 1 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 84fcb6c..3ed1ccb 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -10,6 +10,7 @@ Changes:
* Improved performance of `compound_statements` check; #314 / #522
* Fixed remaining references to `pep8`; #518 / #530
* Added `noqa` support for `maximum_line_length` check; #538
+* Added check E305 for two blank lines after toplevel methods and classes; #400
2.0.0 (2016-05-31)
diff --git a/docs/intro.rst b/docs/intro.rst
index 982b786..0f5132b 100644
--- a/docs/intro.rst
+++ b/docs/intro.rst
@@ -322,6 +322,8 @@ This is the current list of error and warning codes:
+------------+----------------------------------------------------------------------+
| E304 | blank lines found after function decorator |
+------------+----------------------------------------------------------------------+
+| E305 | expected 2 blank lines after end of function or class |
++------------+----------------------------------------------------------------------+
+------------+----------------------------------------------------------------------+
| **E4** | *Import* |
+------------+----------------------------------------------------------------------+
diff --git a/pycodestyle.py b/pycodestyle.py
index b32f16e..a5f5534 100755
--- a/pycodestyle.py
+++ b/pycodestyle.py
@@ -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_unindented_logical_line, 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_unindented_logical_line.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 @@ class Checker(object):
if self.logical_line:
self.previous_indent_level = self.indent_level
self.previous_logical = self.logical_line
+ if not self.indent_level:
+ self.previous_unindented_logical_line = self.logical_line
self.blank_lines = 0
self.tokens = []
@@ -1678,6 +1688,7 @@ class Checker(object):
self.indent_char = None
self.indent_level = self.previous_indent_level = 0
self.previous_logical = ''
+ self.previous_unindented_logical_line = ''
self.tokens = []
self.blank_lines = self.blank_before = 0
parens = 0
diff --git a/testsuite/E12not.py b/testsuite/E12not.py
index e76ef13..18c6a64 100644
--- a/testsuite/E12not.py
+++ b/testsuite/E12not.py
@@ -139,6 +139,7 @@ def long_function_name(
var_four):
print(var_one)
+
if ((row < 0 or self.moduleCount <= row or
col < 0 or self.moduleCount <= col)):
raise Exception("%s,%s - %s" % (row, col, self.moduleCount))
@@ -400,6 +401,7 @@ def unicode2html(s):
.replace('"', '&#34;')
.replace('\n', '<br>\n'))
+
#
parser.add_option('--count', action='store_true',
help="print total number of errors and warnings "
@@ -616,6 +618,7 @@ def other_example():
for key, val in node.items()
))]
+
foo([
'bug'
])
diff --git a/testsuite/E22.py b/testsuite/E22.py
index 9d8efda..ee9dc74 100644
--- a/testsuite/E22.py
+++ b/testsuite/E22.py
@@ -150,6 +150,7 @@ if alpha[:-i]:
def squares(n):
return (i**2 for i in range(n))
+
ENG_PREFIXES = {
-6: "\u03bc", # Greek letter mu
-3: "m",
diff --git a/testsuite/E30.py b/testsuite/E30.py
index d2d7bf3..1471079 100644
--- a/testsuite/E30.py
+++ b/testsuite/E30.py
@@ -88,3 +88,43 @@ def function():
It gives error E303: too many blank lines (3)
"""
#:
+
+#: E305:7:1
+def a():
+ print
+
+ # comment
+
+ # another comment
+a()
+#: E305:8:1
+def a():
+ print
+
+ # comment
+
+ # another comment
+
+try:
+ a()
+except:
+ pass
+#: E305:5:1
+def a():
+ print
+
+# Two spaces before comments, too.
+if a():
+ a()
+#:
+
+#: E305:8:1
+# Example from https://github.com/PyCQA/pycodestyle/issues/400
+import stuff
+
+
+def main():
+ blah, blah
+
+if __name__ == '__main__':
+ main()
diff --git a/testsuite/E30not.py b/testsuite/E30not.py
index 0fd8fb0..ea75057 100644
--- a/testsuite/E30not.py
+++ b/testsuite/E30not.py
@@ -132,3 +132,22 @@ def a():
def b():
pass
+#: Okay
+def foo():
+ pass
+
+
+def bar():
+ pass
+
+
+class Foo(object):
+ pass
+
+
+class Bar(object):
+ pass
+
+
+if __name__ == '__main__':
+ foo()
diff --git a/testsuite/support.py b/testsuite/support.py
index 003f181..cf9abc5 100644
--- a/testsuite/support.py
+++ b/testsuite/support.py
@@ -196,5 +196,6 @@ def run_tests(style):
init_tests(style)
return style.check_files()
+
# nose should not collect these functions
init_tests.__test__ = run_tests.__test__ = False
diff --git a/testsuite/test_all.py b/testsuite/test_all.py
index bd18943..08f9ea9 100644
--- a/testsuite/test_all.py
+++ b/testsuite/test_all.py
@@ -62,5 +62,6 @@ def suite():
def _main():
return unittest.TextTestRunner(verbosity=2).run(suite())
+
if __name__ == '__main__':
sys.exit(not _main())