Handle errors reported in empty files

Some plugins (e.g., flake8-future-import) report errors for empty
files. Those plugins default to reporting the line number as 1 which
caused earlier versions of Flake8 3.0 beta to crash on an IndexError

Closes #157
This commit is contained in:
Ian Cordasco 2016-07-09 20:21:26 -05:00
parent ee44be0c82
commit 57e82688df
No known key found for this signature in database
GPG key ID: 656D3395E4A9791A
2 changed files with 10 additions and 3 deletions

View file

@ -123,8 +123,9 @@ class BaseFormatter(object):
:rtype:
str
"""
if not self.options.show_source:
return None
if not self.options.show_source or error.physical_line is None:
return ''
pointer = (' ' * error.column_number) + '^'
# Physical lines have a newline at the end, no need to add an extra
# one

View file

@ -250,7 +250,13 @@ class FileProcessor(object):
def line_for(self, line_number):
"""Retrieve the physical line at the specified line number."""
return self.lines[line_number - 1]
adjusted_line_number = line_number - 1
# NOTE(sigmavirus24): Some plugins choose to report errors for empty
# files on Line 1. In those casese, we shouldn't bother trying to
# retrieve a physical line (since none exist).
if 0 <= adjusted_line_number < len(self.lines):
return self.lines[adjusted_line_number]
return None
def next_line(self):
"""Get the next line from the list."""