Skip to content

Commit 2ed2baa

Browse files
committed
fix skipping of physical checks when file does not end in newline
1 parent 6a56d22 commit 2ed2baa

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

pycodestyle.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def trailing_blank_lines(physical_line, lines, line_number, total_lines):
264264
However the last line should end with a new line (warning W292).
265265
"""
266266
if line_number == total_lines:
267-
stripped_last_line = physical_line.rstrip()
267+
stripped_last_line = physical_line.rstrip('\r\n')
268268
if physical_line and not stripped_last_line:
269269
return 0, "W391 blank line at end of file"
270270
if stripped_last_line == physical_line:
@@ -2125,21 +2125,30 @@ def generate_tokens(self):
21252125
self.report_error(1, 0, 'E902 %s' % self._io_error, readlines)
21262126
tokengen = tokenize.generate_tokens(self.readline)
21272127
try:
2128+
prev_physical = ''
21282129
for token in tokengen:
21292130
if token[2][0] > self.total_lines:
21302131
return
21312132
self.noqa = token[4] and noqa(token[4])
2132-
self.maybe_check_physical(token)
2133+
self.maybe_check_physical(token, prev_physical)
21332134
yield token
2135+
prev_physical = token[4]
21342136
except (SyntaxError, tokenize.TokenError):
21352137
self.report_invalid_syntax()
21362138

2137-
def maybe_check_physical(self, token):
2139+
def maybe_check_physical(self, token, prev_physical):
21382140
"""If appropriate for token, check current physical line(s)."""
21392141
# Called after every token, but act only on end of line.
2142+
2143+
# a newline token ends a single physical line.
21402144
if _is_eol_token(token):
2141-
# Obviously, a newline token ends a single physical line.
2142-
self.check_physical(token[4])
2145+
# if the file does not end with a newline, the NEWLINE
2146+
# token is inserted by the parser, but it does not contain
2147+
# the previous physical line in `token[4]`
2148+
if token[4] == '':
2149+
self.check_physical(prev_physical)
2150+
else:
2151+
self.check_physical(token[4])
21432152
elif token[0] == tokenize.STRING and '\n' in token[1]:
21442153
# Less obviously, a string that contains newlines is a
21452154
# multiline string, either triple-quoted or with internal

testsuite/W29.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ class Foo(object):
99
#: W291:2:35
1010
'''multiline
1111
string with trailing whitespace'''
12+
#: W291 W292 noeol
13+
x = 1
14+
#: W191 W292 noeol
15+
if False:
16+
pass # indented with tabs
1217
#: W292:1:36 noeol
1318
# This line doesn't have a linefeed
1419
#: W292:1:5 E225:1:2 noeol

testsuite/W39.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Two additional empty lines
66

77

8-
#: W391:4:1 W293:3:1 W293:4:1 noeol
8+
#: W292:4:5 W293:3:1 W293:4:1 noeol
99
# The last lines contain space
1010

1111

0 commit comments

Comments
 (0)