From 30d1796ec6fbe882e10d99b356102f857fbb6c36 Mon Sep 17 00:00:00 2001 From: Jesaja Everling Date: Fri, 26 Oct 2018 15:53:54 +0200 Subject: [PATCH] Changed regex so colon is optional - see #469 --- src/flake8/defaults.py | 2 +- tests/unit/test_violation.py | 61 +++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/flake8/defaults.py b/src/flake8/defaults.py index 61f2571..d84ec56 100644 --- a/src/flake8/defaults.py +++ b/src/flake8/defaults.py @@ -33,7 +33,7 @@ NOQA_INLINE_REGEXP = re.compile( # We do not care about the ``: `` that follows ``noqa`` # We do not care about the casing of ``noqa`` # We want a comma-separated list of errors - r"# noqa(?:: (?P([A-Z][0-9]+(?:[,\s]+)?)+))?", + r"# noqa(:? (?P([A-Z][0-9]+(?:[,\s]+)?)+))?", re.IGNORECASE, ) diff --git a/tests/unit/test_violation.py b/tests/unit/test_violation.py index c4b56b1..19d8fbe 100644 --- a/tests/unit/test_violation.py +++ b/tests/unit/test_violation.py @@ -5,52 +5,63 @@ import pytest from flake8 import style_guide -@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), - ('W123', 'a = 1 # noqa: E111, W123,F821', True), - ('E111', 'a = 1 # noqa: E11,W123,F821', True), - ('E111', 'a = 1 # noqa, analysis:ignore', True), - ('E111', 'a = 1 # noqa analysis:ignore', True), - ('E111', 'a = 1 # noqa - We do not care', True), - ('E111', 'a = 1 # noqa: We do not care', True), -]) +@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), + ("W123", "a = 1 # noqa: E111, W123,F821", True), + ("E111", "a = 1 # noqa: E11,W123,F821", True), + ("E111", "a = 1 # noqa, analysis:ignore", True), + ("E111", "a = 1 # noqa analysis:ignore", True), + ("E111", "a = 1 # noqa - We do not care", True), + ("E111", "a = 1 # noqa: We do not care", True), + ("E111", "a = 1 # noqa E121", False), + ("E111", "a = 1 # noqa W123,E121", False), + ("E111", "a = 1 # noqa W123,E111", True), + ], +) def test_is_inline_ignored(error_code, physical_line, expected_result): """Verify that we detect inline usage of ``# noqa``.""" error = style_guide.Violation( - error_code, 'filename.py', 1, 1, 'error text', None) + error_code, "filename.py", 1, 1, "error text", None + ) # We want `None` to be passed as the physical line so we actually use our # monkey-patched linecache.getline value. - with mock.patch('linecache.getline', return_value=physical_line): + with mock.patch("linecache.getline", return_value=physical_line): assert error.is_inline_ignored(False) is expected_result def test_disable_is_inline_ignored(): """Verify that is_inline_ignored exits immediately if disabling NoQA.""" error = style_guide.Violation( - 'E121', 'filename.py', 1, 1, 'error text', 'line') + "E121", "filename.py", 1, 1, "error text", "line" + ) - with mock.patch('linecache.getline') as getline: + with mock.patch("linecache.getline") as getline: assert error.is_inline_ignored(True) is False assert getline.called is False -@pytest.mark.parametrize('violation_file,violation_line,diff,expected', [ - ('file.py', 10, {}, True), - ('file.py', 1, {'file.py': range(1, 2)}, True), - ('file.py', 10, {'file.py': range(1, 2)}, False), - ('file.py', 1, {'other.py': range(1, 2)}, False), - ('file.py', 10, {'other.py': range(1, 2)}, False), -]) +@pytest.mark.parametrize( + "violation_file,violation_line,diff,expected", + [ + ("file.py", 10, {}, True), + ("file.py", 1, {"file.py": range(1, 2)}, True), + ("file.py", 10, {"file.py": range(1, 2)}, False), + ("file.py", 1, {"other.py": range(1, 2)}, False), + ("file.py", 10, {"other.py": range(1, 2)}, False), + ], +) def test_violation_is_in_diff(violation_file, violation_line, diff, expected): """Verify that we find violations within a diff.""" violation = style_guide.Violation( - 'E001', violation_file, violation_line, 1, 'warning', 'line', + "E001", violation_file, violation_line, 1, "warning", "line" ) assert violation.is_in(diff) is expected