Skip to content

Commit c06b6e7

Browse files
committed
Merge checker W292 with checker W391 for better performance
1 parent 77bd8ae commit c06b6e7

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

pep8.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,23 +171,20 @@ def trailing_whitespace(physical_line):
171171
return 0, "W293 blank line contains whitespace"
172172

173173

174-
def trailing_blank_lines(physical_line, lines, line_number):
174+
def trailing_blank_lines(physical_line, lines, line_number, total_lines):
175175
r"""Trailing blank lines are superfluous.
176176
177177
Okay: spam(1)
178178
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.
186179
187-
Reports warning W292.
180+
However the last line should end with a new line (warning W292).
188181
"""
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"
191188

192189

193190
def maximum_line_length(physical_line, max_line_length, multiline):
@@ -1265,7 +1262,7 @@ def report_invalid_syntax(self):
12651262

12661263
def readline(self):
12671264
"""Get the next line from the input buffer."""
1268-
if self.line_number >= len(self.lines):
1265+
if self.line_number >= self.total_lines:
12691266
return ''
12701267
line = self.lines[self.line_number]
12711268
self.line_number += 1
@@ -1408,6 +1405,7 @@ def maybe_check_physical(self, token):
14081405
def check_all(self, expected=None, line_offset=0):
14091406
"""Run all checks on the input file."""
14101407
self.report.init_file(self.filename, self.lines, expected, line_offset)
1408+
self.total_lines = len(self.lines)
14111409
if self._ast_checks:
14121410
self.check_ast()
14131411
self.line_number = 0

testsuite/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def test_styleguide_checks(self):
246246
pep8style = pep8.StyleGuide(paths=[E11])
247247

248248
# Default lists of checkers
249-
self.assertTrue(len(pep8style.options.physical_checks) > 5)
249+
self.assertTrue(len(pep8style.options.physical_checks) > 4)
250250
self.assertTrue(len(pep8style.options.logical_checks) > 10)
251251
self.assertEqual(len(pep8style.options.ast_checks), 0)
252252

0 commit comments

Comments
 (0)