From fbd5944f15f90114d4d9c2e299ec26a30c646d82 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Fri, 19 Feb 2016 11:02:22 -0600 Subject: [PATCH] Add specific tests for is_inline_ignored Update the logic so someone can use a class in their ``# noqa`` ignore list --- flake8/style_guide.py | 2 +- tests/unit/test_style_guide.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/flake8/style_guide.py b/flake8/style_guide.py index af68dce..6a05249 100644 --- a/flake8/style_guide.py +++ b/flake8/style_guide.py @@ -173,7 +173,7 @@ class StyleGuide(object): return True codes = set(utils.parse_comma_separated_list(codes_str)) - if error.code in codes: + if error.code in codes or error.code.startswith(tuple(codes)): LOG.debug('%r is ignored specifically inline with ``# noqa: %s``', error, codes_str) return True diff --git a/tests/unit/test_style_guide.py b/tests/unit/test_style_guide.py index e02fdc7..4ae4f5f 100644 --- a/tests/unit/test_style_guide.py +++ b/tests/unit/test_style_guide.py @@ -125,6 +125,26 @@ def test_should_report_error(select_list, ignore_list, error_code, expected): assert guide.should_report_error(error_code) is expected +@pytest.mark.parametrize('error_code,physical_line,expected_result', [ + ('E111', 'a = 1', False), + ('E121', 'a = 1 # noqa: E111', False), + ('E121', 'a = 1 # noqa: E111,W123,F821', False), + ('E111', 'a = 1 # noqa: E111,W123,F821', True), + ('W123', 'a = 1 # noqa: E111,W123,F821', True), + ('E111', 'a = 1 # noqa: E11,W123,F821', True), +]) +def test_is_inline_ignored(error_code, physical_line, expected_result): + """Verify that we detect inline usage of ``# noqa``.""" + guide = style_guide.StyleGuide(create_options(select=['E', 'W', 'F']), + arguments=[], + listener_trie=None, + formatter=None) + error = style_guide.Error(error_code, 'filename.py', 1, 1, 'error text') + + with mock.patch('linecache.getline', return_value=physical_line): + assert guide.is_inline_ignored(error) is expected_result + + @pytest.mark.parametrize('select_list,ignore_list,error_code', [ (['E111', 'E121'], [], 'E111'), (['E111', 'E121'], [], 'E121'),