Fix logical checks which report position out of bounds

This commit is contained in:
Anthony Sottile 2020-05-07 11:13:23 -07:00
parent 0c3b8045a7
commit 609010ce7a
2 changed files with 28 additions and 2 deletions

View file

@ -523,8 +523,9 @@ class FileChecker(object):
self.processor.update_checker_state_for(plugin)
results = self.run_check(plugin, logical_line=logical_line) or ()
for offset, text in results:
offset = find_offset(offset, mapping)
line_number, column_offset = offset
line_number, column_offset = find_offset(offset, mapping)
if line_number == column_offset == 0:
LOG.warning("position of error out of bounds: %s", plugin)
self.report(
error_code=None,
line_number=line_number,
@ -674,4 +675,7 @@ def find_offset(offset, mapping):
if offset <= token_offset:
position = token[1]
break
else:
position = (0, 0)
offset = token_offset = 0
return (position[0], position[1] + offset - token_offset)

View file

@ -5,6 +5,7 @@ import pytest
from flake8 import checker
from flake8._compat import importlib_metadata
from flake8.plugins import manager
from flake8.processor import FileProcessor
PHYSICAL_LINE = "# Physical line content"
@ -159,6 +160,27 @@ def test_line_check_results(plugin_target, len_results):
assert file_checker.results == expected
def test_logical_line_offset_out_of_bounds():
"""Ensure that logical line offsets that are out of bounds do not crash."""
@plugin_func
def _logical_line_out_of_bounds(logical_line):
yield 10000, 'L100 test'
file_checker = mock_file_checker_with_plugin(_logical_line_out_of_bounds)
logical_ret = (
'',
'print("xxxxxxxxxxx")',
[(0, (1, 0)), (5, (1, 5)), (6, (1, 6)), (19, (1, 19)), (20, (1, 20))],
)
with mock.patch.object(
FileProcessor, 'build_logical_line', return_value=logical_ret,
):
file_checker.run_logical_checks()
assert file_checker.results == [('L100', 0, 0, 'test', None)]
PLACEHOLDER_CODE = 'some_line = "of" * code'