diff options
author | Florent Xicluna <florent.xicluna@gmail.com> | 2014-03-25 01:39:06 +0100 |
---|---|---|
committer | Florent Xicluna <florent.xicluna@gmail.com> | 2014-03-25 01:39:06 +0100 |
commit | 2336afaf09875f1501f0fb05dc11c477c5c77bc1 (patch) | |
tree | 41a9a17e0d12b28943573f3ddfb43f74f263248f | |
parent | 3d8365a7c8ecd165cef630cc33244e6f3bc9085f (diff) | |
parent | 856d287be3cd8101e8de703b9dd0eef263947e74 (diff) | |
download | pep8-2336afaf09875f1501f0fb05dc11c477c5c77bc1.tar.gz |
Merge, add E265 for space before block comment; issue #190
-rw-r--r-- | CHANGES.txt | 2 | ||||
-rw-r--r-- | docs/intro.rst | 2 | ||||
-rwxr-xr-x | pep8.py | 49 | ||||
-rw-r--r-- | testsuite/E11.py | 4 | ||||
-rw-r--r-- | testsuite/E26.py | 25 | ||||
-rw-r--r-- | testsuite/test_api.py | 4 | ||||
-rw-r--r-- | testsuite/test_shell.py | 2 |
7 files changed, 69 insertions, 19 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index b9767ac..c2ff646 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -8,6 +8,8 @@ Changelog * Report E129 instead of E125 for visually indented line with same indent as next logical line. (Issue #126) +* Report E265 for space before block comment. (Issue #190) + * Report E713 and E714 when operators ``not in`` and ``is not`` are recommended. (Issue #236) diff --git a/docs/intro.rst b/docs/intro.rst index 1a50424..7d2f16d 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -259,6 +259,8 @@ This is the current list of error and warning codes: +----------+----------------------------------------------------------------------+ | E262 | inline comment should start with '# ' | +----------+----------------------------------------------------------------------+ +| E265 | block comment should start with '# ' | ++----------+----------------------------------------------------------------------+ +----------+----------------------------------------------------------------------+ | E271 | multiple spaces after keyword | +----------+----------------------------------------------------------------------+ @@ -782,7 +782,7 @@ def whitespace_around_named_parameter_equals(logical_line, tokens): prev_end = end -def whitespace_before_inline_comment(logical_line, tokens): +def whitespace_before_comment(logical_line, tokens): """ Separate inline comments by at least two spaces. @@ -790,23 +790,33 @@ def whitespace_before_inline_comment(logical_line, tokens): comments should be separated by at least two spaces from the statement. They should start with a # and a single space. + Each line of a block comment starts with a # and a single space + (unless it is indented text inside the comment). + Okay: x = x + 1 # Increment x Okay: x = x + 1 # Increment x + Okay: # Block comment E261: x = x + 1 # Increment x E262: x = x + 1 #Increment x E262: x = x + 1 # Increment x + E265: #Block comment """ prev_end = (0, 0) for token_type, text, start, end, line in tokens: if token_type == tokenize.COMMENT: - if not line[:start[1]].strip(): - continue - if prev_end[0] == start[0] and start[1] < prev_end[1] + 2: - yield (prev_end, - "E261 at least two spaces before inline comment") + inline_comment = line[:start[1]].strip() + if inline_comment: + if prev_end[0] == start[0] and start[1] < prev_end[1] + 2: + yield (prev_end, + "E261 at least two spaces before inline comment") symbol, sp, comment = text.partition(' ') - if symbol not in ('#', '#:') or comment[:1].isspace(): - yield start, "E262 inline comment should start with '# '" + bad_prefix = symbol not in ('#', '#:') + if inline_comment: + if bad_prefix or comment[:1].isspace(): + yield start, "E262 inline comment should start with '# '" + elif bad_prefix: + if text.rstrip('#') and (start[0] > 1 or symbol[1] != '!'): + yield start, "E265 block comment should start with '# '" elif token_type != tokenize.NL: prev_end = end @@ -1371,9 +1381,9 @@ class Checker(object): """ self.build_tokens_line() self.report.increment_logical_line() - first_line = self.lines[self.mapping[0][1][2][0] - 1] - indent = first_line[:self.mapping[0][1][2][1]] - self.previous_indent_level = self.indent_level + token0 = self.mapping[0][1] if self.mapping else self.tokens[0] + first_line = self.lines[token0[2][0] - 1] + indent = first_line[:token0[2][1]] self.indent_level = expand_indent(indent) if self.verbose >= 2: print(self.logical_line[:80].rstrip()) @@ -1385,12 +1395,17 @@ class Checker(object): if isinstance(offset, tuple): (orig_number, orig_offset) = offset else: + orig_number = token0[2][0] + orig_offset = token0[2][1] + offset for token_offset, token in self.mapping: if offset >= token_offset: orig_number = token[2][0] orig_offset = (token[2][1] + offset - token_offset) self.report_error(orig_number, orig_offset, text, check) - self.previous_logical = self.logical_line + if self.logical_line: + self.previous_indent_level = self.indent_level + self.previous_logical = self.logical_line + self.tokens = [] def check_ast(self): try: @@ -1423,6 +1438,7 @@ class Checker(object): self.line_number = 0 self.indent_char = None self.indent_level = 0 + self.previous_indent_level = 0 self.previous_logical = '' self.tokens = [] self.blank_lines = blank_lines_before_comment = 0 @@ -1447,20 +1463,23 @@ class Checker(object): if self.blank_lines < blank_lines_before_comment: self.blank_lines = blank_lines_before_comment self.check_logical() - self.tokens = [] self.blank_lines = blank_lines_before_comment = 0 elif token_type == tokenize.NL: if len(self.tokens) == 1: # The physical line contains only this token. self.blank_lines += 1 - self.tokens = [] + del self.tokens[0] + else: + self.check_logical() elif token_type == tokenize.COMMENT and len(self.tokens) == 1: if blank_lines_before_comment < self.blank_lines: blank_lines_before_comment = self.blank_lines self.blank_lines = 0 if COMMENT_WITH_NL: # The comment also ends a physical line - self.tokens = [] + text = text.rstrip('\r\n') + self.tokens = [(token_type, text) + token[2:]] + self.check_logical() return self.report.get_file_results() diff --git a/testsuite/E11.py b/testsuite/E11.py index 8735e25..98b9431 100644 --- a/testsuite/E11.py +++ b/testsuite/E11.py @@ -10,3 +10,7 @@ print #: E113 print print +#: E111 E113 +mimetype = 'application/x-directory' + # 'httpd/unix-directory' +create_date = False diff --git a/testsuite/E26.py b/testsuite/E26.py index 984efb5..d838e70 100644 --- a/testsuite/E26.py +++ b/testsuite/E26.py @@ -6,8 +6,31 @@ x = x + 1 #Increment x x = x + 1 # Increment x #: E262 x = y + 1 #: Increment x +#: E265 +#Block comment +a = 1 +#: E265 +m = 42 +#! This is important +mx = 42 - 42 #: Okay +#!/usr/bin/env python + pass # an inline comment x = x + 1 # Increment x y = y + 1 #: Increment x -#: + +# Block comment +a = 1 + +# Block comment1 + +# Block comment2 +aaa = 1 + + +# example of docstring (not parsed) +def oof(): + """ + #foo not parsed + """ diff --git a/testsuite/test_api.py b/testsuite/test_api.py index 313fd93..95f99d9 100644 --- a/testsuite/test_api.py +++ b/testsuite/test_api.py @@ -124,7 +124,7 @@ class APITestCase(unittest.TestCase): report = pep8.StyleGuide().check_files([E11]) stdout = sys.stdout.getvalue().splitlines() self.assertEqual(len(stdout), report.total_errors) - self.assertEqual(report.total_errors, 4) + self.assertEqual(report.total_errors, 6) self.assertFalse(sys.stderr) self.reset() @@ -132,7 +132,7 @@ class APITestCase(unittest.TestCase): report = pep8.StyleGuide(paths=[E11]).check_files() stdout = sys.stdout.getvalue().splitlines() self.assertEqual(len(stdout), report.total_errors) - self.assertEqual(report.total_errors, 4) + self.assertEqual(report.total_errors, 6) self.assertFalse(sys.stderr) self.reset() diff --git a/testsuite/test_shell.py b/testsuite/test_shell.py index 1f12b44..0313c0c 100644 --- a/testsuite/test_shell.py +++ b/testsuite/test_shell.py @@ -75,7 +75,7 @@ class ShellTestCase(unittest.TestCase): stdout = stdout.splitlines() self.assertEqual(errcode, 1) self.assertFalse(stderr) - self.assertEqual(len(stdout), 4) + self.assertEqual(len(stdout), 6) for line, num, col in zip(stdout, (3, 6, 9, 12), (3, 6, 1, 5)): path, x, y, msg = line.split(':') self.assertTrue(path.endswith(E11)) |