Merge branch 'show_source_with_tabs' into 'master'

Fix --show-source when indented with tabs

Closes #563

See merge request pycqa/flake8!339
This commit is contained in:
Anthony Sottile 2019-08-10 18:44:04 +00:00
commit 03cb85f556
2 changed files with 24 additions and 11 deletions

View file

@ -171,10 +171,13 @@ class BaseFormatter(object):
# Because column numbers are 1-indexed, we need to remove one to get
# the proper number of space characters.
pointer = (" " * (error.column_number - 1)) + "^"
indent = "".join(
c if c.isspace() else " "
for c in error.physical_line[: error.column_number - 1]
)
# Physical lines have a newline at the end, no need to add an extra
# one
return error.physical_line + pointer
return "{}{}^".format(error.physical_line, indent)
def _write(self, output): # type: (str) -> None
"""Handle logic of whether to use an output file or print()."""

View file

@ -65,19 +65,29 @@ def test_show_source_returns_nothing_when_there_is_source():
) == ''
@pytest.mark.parametrize('line, column', [
('x=1\n', 2),
(' x=(1\n +2)\n', 5),
# TODO(sigmavirus24): Add more examples
@pytest.mark.parametrize(('line1', 'line2', 'column'), [
(
'x=1\n',
' ^',
2,
),
(
' x=(1\n +2)\n',
' ^',
5,
),
(
'\tx\t=\ty\n',
'\t \t \t^',
6,
),
])
def test_show_source_updates_physical_line_appropriately(line, column):
def test_show_source_updates_physical_line_appropriately(line1, line2, column):
"""Ensure the error column is appropriately indicated."""
formatter = base.BaseFormatter(options(show_source=True))
error = style_guide.Violation('A000', 'file.py', 1, column, 'error', line)
error = style_guide.Violation('A000', 'file.py', 1, column, 'error', line1)
output = formatter.show_source(error)
assert output
_, pointer = output.rsplit('\n', 1)
assert pointer.count(' ') == (column - 1)
assert output == line1 + line2
@pytest.mark.parametrize('tee', [False, True])