From 6eca38f2f2a15765aecef010f878aa17fdbe1db7 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Tue, 26 Jul 2016 19:15:08 -0500 Subject: [PATCH] Fix zero-indexed column numbering We accidentally changed column numbering from one-indexed to zero-indexed. --- docs/source/release-notes/3.0.2.rst | 3 +++ src/flake8/formatting/base.py | 4 +++- src/flake8/style_guide.py | 4 +++- tests/unit/test_base_formatter.py | 2 +- tests/unit/test_style_guide.py | 2 +- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/docs/source/release-notes/3.0.2.rst b/docs/source/release-notes/3.0.2.rst index 7bdb803..4fbc0a4 100644 --- a/docs/source/release-notes/3.0.2.rst +++ b/docs/source/release-notes/3.0.2.rst @@ -3,6 +3,9 @@ - Fix local config file discovery. (See also `GitLab#181`_) +- Fix indexing of column numbers. We accidentally were starting column indices + at 0 instead of 1. + .. links .. _GitLab#181: diff --git a/src/flake8/formatting/base.py b/src/flake8/formatting/base.py index 2ac0ed8..336bf50 100644 --- a/src/flake8/formatting/base.py +++ b/src/flake8/formatting/base.py @@ -139,7 +139,9 @@ class BaseFormatter(object): if not self.options.show_source or error.physical_line is None: return '' - pointer = (' ' * error.column_number) + '^' + # Because column numbers are 1-indexed, we need to remove one to get + # the proper number of space characters. + pointer = (' ' * (error.column_number - 1)) + '^' # Physical lines have a newline at the end, no need to add an extra # one return error.physical_line + pointer diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py index 950d722..d9a6285 100644 --- a/src/flake8/style_guide.py +++ b/src/flake8/style_guide.py @@ -258,7 +258,9 @@ class StyleGuide(object): :rtype: int """ - error = Error(code, filename, line_number, column_number, text, + # NOTE(sigmavirus24): Apparently we're provided with 0-indexed column + # numbers so we have to offset that here. + error = Error(code, filename, line_number, column_number + 1, text, physical_line) error_is_selected = (self.should_report_error(error.code) is Decision.Selected) diff --git a/tests/unit/test_base_formatter.py b/tests/unit/test_base_formatter.py index f148806..ae5c12a 100644 --- a/tests/unit/test_base_formatter.py +++ b/tests/unit/test_base_formatter.py @@ -73,7 +73,7 @@ def test_show_source_updates_physical_line_appropriately(line, column): error = style_guide.Error('A000', 'file.py', 1, column, 'error', line) output = formatter.show_source(error) _, pointer = output.rsplit('\n', 1) - assert pointer.count(' ') == column + assert pointer.count(' ') == (column - 1) def test_write_uses_an_output_file(): diff --git a/tests/unit/test_style_guide.py b/tests/unit/test_style_guide.py index a38c657..4efbca5 100644 --- a/tests/unit/test_style_guide.py +++ b/tests/unit/test_style_guide.py @@ -176,7 +176,7 @@ def test_handle_error_notifies_listeners(select_list, ignore_list, error_code): formatter=formatter) with mock.patch('linecache.getline', return_value=''): - guide.handle_error(error_code, 'stdin', 1, 1, 'error found') + guide.handle_error(error_code, 'stdin', 1, 0, 'error found') error = style_guide.Error(error_code, 'stdin', 1, 1, 'error found', None) listener_trie.notify.assert_called_once_with(error_code, error)