diff options
author | Florent Xicluna <florent.xicluna@gmail.com> | 2013-05-01 23:48:21 +0200 |
---|---|---|
committer | Florent Xicluna <florent.xicluna@gmail.com> | 2013-05-01 23:48:21 +0200 |
commit | 52ba8c543de4dee17d90fc4a566b18b9e0771b3d (patch) | |
tree | 3e362b1895d44ce13135793fdba0c5e9192dcdc1 | |
parent | 00aff369146b3955feaa83fe7a0136d4b24a6c08 (diff) | |
download | pep8-52ba8c543de4dee17d90fc4a566b18b9e0771b3d.tar.gz |
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 | 47 | ||||
-rw-r--r-- | testsuite/E11.py | 4 | ||||
-rw-r--r-- | testsuite/test_api.py | 4 | ||||
-rw-r--r-- | testsuite/test_shell.py | 2 |
6 files changed, 43 insertions, 18 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 17fad0d..9d379da 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -22,6 +22,8 @@ Changelog * Fix false positive E261/E262 when the file contains a BOM. (Issue #193) +* Add E265 for space before block comment. (Issue #190) + 1.4.5 (2013-03-06) ------------------ diff --git a/docs/intro.rst b/docs/intro.rst index d73a3c8..6d97b5a 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -252,6 +252,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 | +----------+----------------------------------------------------------------------+ @@ -764,7 +764,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. @@ -772,23 +772,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 @@ -1307,9 +1317,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()) @@ -1321,12 +1331,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: @@ -1359,6 +1374,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 @@ -1383,20 +1399,21 @@ 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 = [] + 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/test_api.py b/testsuite/test_api.py index 3fa35f1..bff533f 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)) |