@@ -171,23 +171,20 @@ def trailing_whitespace(physical_line):
171
171
return 0 , "W293 blank line contains whitespace"
172
172
173
173
174
- def trailing_blank_lines (physical_line , lines , line_number ):
174
+ def trailing_blank_lines (physical_line , lines , line_number , total_lines ):
175
175
r"""Trailing blank lines are superfluous.
176
176
177
177
Okay: spam(1)
178
178
W391: spam(1)\n
179
- """
180
- if not physical_line .rstrip () and line_number == len (lines ):
181
- return 0 , "W391 blank line at end of file"
182
-
183
-
184
- def missing_newline (physical_line ):
185
- r"""The last line should have a newline.
186
179
187
- Reports warning W292.
180
+ However the last line should end with a new line ( warning W292) .
188
181
"""
189
- if physical_line .rstrip () == physical_line :
190
- return len (physical_line ), "W292 no newline at end of file"
182
+ if line_number == total_lines :
183
+ stripped_last_line = physical_line .rstrip ()
184
+ if not stripped_last_line :
185
+ return 0 , "W391 blank line at end of file"
186
+ if stripped_last_line == physical_line :
187
+ return len (physical_line ), "W292 no newline at end of file"
191
188
192
189
193
190
def maximum_line_length (physical_line , max_line_length , multiline ):
@@ -1265,7 +1262,7 @@ def report_invalid_syntax(self):
1265
1262
1266
1263
def readline (self ):
1267
1264
"""Get the next line from the input buffer."""
1268
- if self .line_number >= len ( self .lines ) :
1265
+ if self .line_number >= self .total_lines :
1269
1266
return ''
1270
1267
line = self .lines [self .line_number ]
1271
1268
self .line_number += 1
@@ -1408,6 +1405,7 @@ def maybe_check_physical(self, token):
1408
1405
def check_all (self , expected = None , line_offset = 0 ):
1409
1406
"""Run all checks on the input file."""
1410
1407
self .report .init_file (self .filename , self .lines , expected , line_offset )
1408
+ self .total_lines = len (self .lines )
1411
1409
if self ._ast_checks :
1412
1410
self .check_ast ()
1413
1411
self .line_number = 0
0 commit comments