diff --git a/pep8.py b/pep8.py index 4d993dab..e4f65ac3 100755 --- a/pep8.py +++ b/pep8.py @@ -234,7 +234,8 @@ def maximum_line_length(physical_line, max_line_length, multiline): 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. @@ -253,6 +254,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 @@ -268,6 +270,9 @@ 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 before, found %d" % blank_before def extraneous_whitespace(logical_line): @@ -1301,6 +1306,8 @@ def filename_match(filename, patterns, default=True): def _is_eol_token(token): return token[0] in NEWLINE or token[4][token[3][1]:].lstrip() == '\\\n' + + if COMMENT_WITH_NL: def _is_eol_token(token, _eol_token=_is_eol_token): return _eol_token(token) or (token[0] == tokenize.COMMENT and @@ -1340,6 +1347,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() @@ -1497,6 +1506,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 self.blank_lines = 0 self.tokens = [] @@ -1566,6 +1577,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 @@ -2116,5 +2128,6 @@ def _main(): sys.stderr.write(str(report.total_errors) + '\n') sys.exit(1) + if __name__ == '__main__': _main() diff --git a/testsuite/E12not.py b/testsuite/E12not.py index e76ef134..18c6a646 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('"', '"') .replace('\n', '
\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 9d8efda5..ee9dc74a 100644 --- a/testsuite/E22.py +++ b/testsuite/E22.py @@ -150,6 +150,7 @@ def halves(n): 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 d2d7bf35..1aa11379 100644 --- a/testsuite/E30.py +++ b/testsuite/E30.py @@ -88,3 +88,32 @@ 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() +#: diff --git a/testsuite/support.py b/testsuite/support.py index 6bc795d7..3bcd7a2a 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 50e2cb9f..6f9cb380 100644 --- a/testsuite/test_all.py +++ b/testsuite/test_all.py @@ -59,5 +59,6 @@ def suite(): def _main(): return unittest.TextTestRunner(verbosity=2).run(suite()) + if __name__ == '__main__': sys.exit(not _main())