From 1631ab8ac719d1c2ab39df1bc8c41028683ef23e Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Sat, 27 Aug 2016 19:44:24 -0500 Subject: [PATCH 1/2] Add failing test for NoneType in handle_error There are rare cases when StyleGuide#handle_error might receive None as the column_number. This adds the failing test to ensure we don't regress the correct behaviour. Related-to #214 --- tests/unit/test_style_guide.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/unit/test_style_guide.py b/tests/unit/test_style_guide.py index 64a7fcd..8e714b7 100644 --- a/tests/unit/test_style_guide.py +++ b/tests/unit/test_style_guide.py @@ -185,6 +185,19 @@ def test_handle_error_notifies_listeners(select_list, ignore_list, error_code): formatter.handle.assert_called_once_with(error) +def test_handle_error_does_not_raise_type_errors(): + """Verify that we handle our inputs better.""" + listener_trie = mock.create_autospec(notifier.Notifier, instance=True) + formatter = mock.create_autospec(base.BaseFormatter, instance=True) + guide = style_guide.StyleGuide(create_options(select=['T111'], ignore=[]), + listener_trie=listener_trie, + formatter=formatter) + + assert 1 == guide.handle_error( + 'T111', 'file.py', 1, None, 'error found', 'a = 1' + ) + + @pytest.mark.parametrize('select_list,ignore_list,error_code', [ (['E111', 'E121'], [], 'E122'), (['E11', 'E12'], [], 'E132'), From 585628875d29f4a46d8adbccc0e1bfa7b3a4a347 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Sat, 27 Aug 2016 19:53:14 -0500 Subject: [PATCH 2/2] Ensure column_number is always an integer When a SyntaxError is raised, column_number may be None. Unfortunately, it's not obvious where that None comes from so we must handle it in handle_error. Closes #214 --- src/flake8/style_guide.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py index a1ab473..4d8950c 100644 --- a/src/flake8/style_guide.py +++ b/src/flake8/style_guide.py @@ -253,7 +253,10 @@ class StyleGuide(object): int """ # NOTE(sigmavirus24): Apparently we're provided with 0-indexed column - # numbers so we have to offset that here. + # numbers so we have to offset that here. Also, if a SyntaxError is + # caught, column_number may be None. + if not column_number: + column_number = 0 error = Error(code, filename, line_number, column_number + 1, text, physical_line) error_is_selected = (self.should_report_error(error.code) is