mirror of
https://github.com/PyCQA/flake8.git
synced 2026-03-30 18:56:53 +00:00
Merge branch 'bug/157' into 'master'
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
This commit is contained in:
commit
edd84fba52
3 changed files with 19 additions and 4 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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."""
|
||||
|
|
|
|||
|
|
@ -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', [
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue