Skip to content

Commit

Permalink
Merge branch 'bug/157' into 'master'
Browse files Browse the repository at this point in the history
Handle errors reported in empty files

*Description of changes*

Some plugins return errors in empty files which previously caused an IndexError.

*Related to:*  #157 

See merge request !71
  • Loading branch information
sigmavirus24 committed Jul 10, 2016
2 parents 7ea6da2 + e977c66 commit edd84fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/flake8/formatting/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ def show_source(self, error):
: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
Expand Down
8 changes: 7 additions & 1 deletion src/flake8/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,13 @@ def generate_tokens(self):

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."""
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/test_base_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ def test_show_source_returns_nothing_when_not_showing_source():
formatter = base.BaseFormatter(options(show_source=False))
assert formatter.show_source(
style_guide.Error('A000', 'file.py', 1, 1, 'error text', 'line')
) is None
) is ''


def test_show_source_returns_nothing_when_there_is_source():
"""Ensure we return nothing when there is no line."""
formatter = base.BaseFormatter(options(show_source=True))
assert formatter.show_source(
style_guide.Error('A000', 'file.py', 1, 1, 'error text', None)
) is ''


@pytest.mark.parametrize('line, column', [
Expand Down

0 comments on commit edd84fb

Please sign in to comment.