Skip to content

Commit f45d302

Browse files
committed
Report correct line number for E303 with comments; issue #60
1 parent 4178c7e commit f45d302

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Changes:
1111

1212
Bug fixes:
1313

14+
* Report correct line number for E303 with comments. (Issue #60)
15+
1416
* Fix line number reported for multi-line strings. (Issue #220)
1517

1618
* Fix false positive E121/E126 with multi-line strings. (Issue #265)

pep8.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def maximum_line_length(physical_line, max_line_length, multiline):
227227

228228

229229
def blank_lines(logical_line, blank_lines, indent_level, line_number,
230-
previous_logical, previous_indent_level):
230+
blank_before, previous_logical, previous_indent_level):
231231
r"""Separate top-level function and class definitions with two blank lines.
232232
233233
Method definitions inside a class are separated by a single blank line.
@@ -256,11 +256,11 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
256256
yield 0, "E303 too many blank lines (%d)" % blank_lines
257257
elif logical_line.startswith(('def ', 'class ', '@')):
258258
if indent_level:
259-
if not (blank_lines or previous_indent_level < indent_level or
259+
if not (blank_before or previous_indent_level < indent_level or
260260
DOCSTRING_REGEX.match(previous_logical)):
261261
yield 0, "E301 expected 1 blank line, found 0"
262-
elif blank_lines != 2:
263-
yield 0, "E302 expected 2 blank lines, found %d" % blank_lines
262+
elif blank_before != 2:
263+
yield 0, "E302 expected 2 blank lines, found %d" % blank_before
264264

265265

266266
def extraneous_whitespace(logical_line):
@@ -1339,6 +1339,8 @@ def check_logical(self):
13391339
(start_row, start_col) = mapping[0][1][2]
13401340
start_line = self.lines[start_row - 1]
13411341
self.indent_level = expand_indent(start_line[:start_col])
1342+
if self.blank_before < self.blank_lines:
1343+
self.blank_before = self.blank_lines
13421344
if self.verbose >= 2:
13431345
print(self.logical_line[:80].rstrip())
13441346
for name, check, argument_names in self._logical_checks:
@@ -1358,6 +1360,7 @@ def check_logical(self):
13581360
if self.logical_line:
13591361
self.previous_indent_level = self.indent_level
13601362
self.previous_logical = self.logical_line
1363+
self.blank_lines = 0
13611364
self.tokens = []
13621365

13631366
def check_ast(self):
@@ -1421,11 +1424,10 @@ def check_all(self, expected=None, line_offset=0):
14211424
self.check_ast()
14221425
self.line_number = 0
14231426
self.indent_char = None
1424-
self.indent_level = 0
1425-
self.previous_indent_level = 0
1427+
self.indent_level = self.previous_indent_level = 0
14261428
self.previous_logical = ''
14271429
self.tokens = []
1428-
self.blank_lines = blank_lines_before_comment = 0
1430+
self.blank_lines = self.blank_before = 0
14291431
parens = 0
14301432
for token in self.generate_tokens():
14311433
self.tokens.append(token)
@@ -1444,22 +1446,17 @@ def check_all(self, expected=None, line_offset=0):
14441446
parens -= 1
14451447
elif not parens:
14461448
if token_type == tokenize.NEWLINE:
1447-
if self.blank_lines < blank_lines_before_comment:
1448-
self.blank_lines = blank_lines_before_comment
14491449
self.check_logical()
1450-
self.blank_lines = blank_lines_before_comment = 0
1450+
self.blank_before = 0
14511451
elif token_type == tokenize.NL:
14521452
if len(self.tokens) == 1:
14531453
# The physical line contains only this token.
14541454
self.blank_lines += 1
14551455
del self.tokens[0]
14561456
else:
14571457
self.check_logical()
1458-
elif token_type == tokenize.COMMENT and len(self.tokens) == 1:
1459-
if blank_lines_before_comment < self.blank_lines:
1460-
blank_lines_before_comment = self.blank_lines
1461-
self.blank_lines = 0
1462-
if COMMENT_WITH_NL:
1458+
elif COMMENT_WITH_NL and token_type == tokenize.COMMENT:
1459+
if len(self.tokens) == 1:
14631460
# The comment also ends a physical line
14641461
text = text.rstrip('\r\n')
14651462
self.tokens = [(token_type, text) + token[2:]]

testsuite/E30.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#: E301
1+
#: E301:5:5
22
class X:
33

44
def a():
55
pass
66
def b():
77
pass
8-
#: E301
8+
#: E301:6:5
99
class X:
1010

1111
def a():
@@ -16,26 +16,26 @@ def b():
1616
#:
1717

1818

19-
#: E302
19+
#: E302:3:1
2020
#!python
2121
# -*- coding: utf-8 -*-
2222
def a():
2323
pass
24-
#: E302
24+
#: E302:2:1
2525
"""Main module."""
2626
def _main():
2727
pass
28-
#: E302
28+
#: E302:2:1
2929
import sys
3030
def get_sys_path():
3131
return sys.path
32-
#: E302
32+
#: E302:4:1
3333
def a():
3434
pass
3535

3636
def b():
3737
pass
38-
#: E302
38+
#: E302:6:1
3939
def a():
4040
pass
4141

@@ -46,32 +46,35 @@ def b():
4646
#:
4747

4848

49-
#: E303
49+
#: E303:5:1
5050
print
5151

5252

5353

5454
print
55-
#: E303
55+
#: E303:5:1
5656
print
5757

5858

5959

6060
# comment
6161

6262
print
63-
#: E303
63+
#: E303:5:5 E303:8:5
6464
def a():
6565
print
6666

6767

6868
# comment
6969

70+
71+
# another comment
72+
7073
print
7174
#:
7275

7376

74-
#: E304
77+
#: E304:3:1
7578
@decorator
7679

7780
def function():

0 commit comments

Comments
 (0)