From 9a45593aae18b2bd8ae9e02e0ce4ee7147b570dd Mon Sep 17 00:00:00 2001 From: Byeonghoon Yoo Date: Tue, 5 Nov 2019 21:54:24 +0900 Subject: [PATCH 1/6] Add disable_noqa attribute to FileProcessor --- src/flake8/processor.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/flake8/processor.py b/src/flake8/processor.py index ea8d3e2..3ee6dfa 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -36,6 +36,7 @@ class FileProcessor(object): - :attr:`blank_before` - :attr:`blank_lines` - :attr:`checker_state` + - :attr:`disable_noqa` - :attr:`indent_char` - :attr:`indent_level` - :attr:`line_number` @@ -77,6 +78,8 @@ class FileProcessor(object): self._checker_states = {} # type: Dict[str, Dict[Any, Any]] #: Current checker state self.checker_state = {} # type: Dict[Any, Any] + #: Disable all noqa comments + self.disable_noqa = options.disable_noqa # type: bool #: User provided option for hang closing self.hang_closing = options.hang_closing #: Character used for indentation From dd411e95bcb52e3e85c028bc0d1f5158658fc19b Mon Sep 17 00:00:00 2001 From: Byeonghoon Yoo Date: Tue, 5 Nov 2019 21:59:19 +0900 Subject: [PATCH 2/6] Fix should_ignore_file() to handle disable-noqa configuration --- src/flake8/processor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flake8/processor.py b/src/flake8/processor.py index 3ee6dfa..1253e7a 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -349,7 +349,7 @@ class FileProcessor(object): :rtype: bool """ - if any(defaults.NOQA_FILE.match(line) for line in self.lines): + if not self.disable_noqa and any(defaults.NOQA_FILE.match(line) for line in self.lines): return True elif any(defaults.NOQA_FILE.search(line) for line in self.lines): LOG.warning( From 29d8b112093f5d564afe09b2a6f6c618a3cb2643 Mon Sep 17 00:00:00 2001 From: Byeonghoon Yoo Date: Tue, 5 Nov 2019 22:00:05 +0900 Subject: [PATCH 3/6] Add unit test for dd411e95 --- tests/unit/conftest.py | 1 + tests/unit/test_file_processor.py | 33 +++++++++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 3541ec3..eb76f98 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -11,6 +11,7 @@ def options_from(**kwargs): kwargs.setdefault('max_doc_length', None) kwargs.setdefault('verbose', False) kwargs.setdefault('stdin_display_name', 'stdin') + kwargs.setdefault('disable_noqa', False) return argparse.Namespace(**kwargs) diff --git a/tests/unit/test_file_processor.py b/tests/unit/test_file_processor.py index afae77d..ad2f5bd 100644 --- a/tests/unit/test_file_processor.py +++ b/tests/unit/test_file_processor.py @@ -57,21 +57,28 @@ def test_strip_utf_bom(first_line, default_options): assert file_processor.lines[0] == '"""Module docstring."""\n' -@pytest.mark.parametrize('lines, expected', [ - (['\xEF\xBB\xBF"""Module docstring."""\n'], False), - ([u'\uFEFF"""Module docstring."""\n'], False), - (['#!/usr/bin/python', '# flake8 is great', 'a = 1'], False), - (['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], True), - (['#!/usr/bin/python', '# flake8:noqa', 'a = 1'], True), - (['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], True), - (['# flake8:noqa', '#!/usr/bin/python', 'a = 1'], True), - (['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], True), - (['#!/usr/bin/python', 'a = 1', '# flake8:noqa'], True), - (['#!/usr/bin/python', 'a = 1 # flake8: noqa'], False), - (['#!/usr/bin/python', 'a = 1 # flake8:noqa'], False), +@pytest.mark.parametrize('lines, expected, disable_noqa', [ + (['\xEF\xBB\xBF"""Module docstring."""\n'], False, False), + ([u'\uFEFF"""Module docstring."""\n'], False, False), + (['#!/usr/bin/python', '# flake8 is great', 'a = 1'], False, False), + (['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], True, False), + (['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], False, True), + (['#!/usr/bin/python', '# flake8:noqa', 'a = 1'], True, False), + (['#!/usr/bin/python', '# flake8:noqa', 'a = 1'], False, True), + (['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], True, False), + (['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], False, True), + (['# flake8:noqa', '#!/usr/bin/python', 'a = 1'], True, False), + (['# flake8:noqa', '#!/usr/bin/python', 'a = 1'], False, True), + (['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], True, False), + (['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], False, True), + (['#!/usr/bin/python', 'a = 1', '# flake8:noqa'], True, False), + (['#!/usr/bin/python', 'a = 1', '# flake8:noqa'], False, True), + (['#!/usr/bin/python', 'a = 1 # flake8: noqa'], False, False), + (['#!/usr/bin/python', 'a = 1 # flake8:noqa'], False, False), ]) -def test_should_ignore_file(lines, expected, default_options): +def test_should_ignore_file(lines, expected, disable_noqa, default_options): """Verify that we ignore a file if told to.""" + default_options.disable_noqa = disable_noqa file_processor = processor.FileProcessor('-', default_options, lines) assert file_processor.should_ignore_file() is expected From d23f77d06e8d12f961b23b7a5be0c24c62f1417e Mon Sep 17 00:00:00 2001 From: Byeonghoon Yoo Date: Tue, 5 Nov 2019 22:16:18 +0900 Subject: [PATCH 4/6] Fix lint error --- src/flake8/processor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/flake8/processor.py b/src/flake8/processor.py index 1253e7a..d92f0ba 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -349,7 +349,8 @@ class FileProcessor(object): :rtype: bool """ - if not self.disable_noqa and any(defaults.NOQA_FILE.match(line) for line in self.lines): + if not self.disable_noqa \ + and any(defaults.NOQA_FILE.match(line) for line in self.lines): return True elif any(defaults.NOQA_FILE.search(line) for line in self.lines): LOG.warning( From 705c16a2686ef561112854f180b7c79377049922 Mon Sep 17 00:00:00 2001 From: Byeonghoon Yoo Date: Wed, 6 Nov 2019 00:28:09 +0900 Subject: [PATCH 5/6] Fix codes --- src/flake8/processor.py | 9 +++--- tests/unit/test_file_processor.py | 50 ++++++++++++++++++------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/flake8/processor.py b/src/flake8/processor.py index d92f0ba..0fd650b 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -36,7 +36,6 @@ class FileProcessor(object): - :attr:`blank_before` - :attr:`blank_lines` - :attr:`checker_state` - - :attr:`disable_noqa` - :attr:`indent_char` - :attr:`indent_level` - :attr:`line_number` @@ -78,8 +77,6 @@ class FileProcessor(object): self._checker_states = {} # type: Dict[str, Dict[Any, Any]] #: Current checker state self.checker_state = {} # type: Dict[Any, Any] - #: Disable all noqa comments - self.disable_noqa = options.disable_noqa # type: bool #: User provided option for hang closing self.hang_closing = options.hang_closing #: Character used for indentation @@ -349,8 +346,10 @@ class FileProcessor(object): :rtype: bool """ - if not self.disable_noqa \ - and any(defaults.NOQA_FILE.match(line) for line in self.lines): + if ( + not self.options.disable_noqa + and any(defaults.NOQA_FILE.match(line) for line in self.lines) + ): return True elif any(defaults.NOQA_FILE.search(line) for line in self.lines): LOG.warning( diff --git a/tests/unit/test_file_processor.py b/tests/unit/test_file_processor.py index ad2f5bd..9acd5ae 100644 --- a/tests/unit/test_file_processor.py +++ b/tests/unit/test_file_processor.py @@ -57,32 +57,42 @@ def test_strip_utf_bom(first_line, default_options): assert file_processor.lines[0] == '"""Module docstring."""\n' -@pytest.mark.parametrize('lines, expected, disable_noqa', [ - (['\xEF\xBB\xBF"""Module docstring."""\n'], False, False), - ([u'\uFEFF"""Module docstring."""\n'], False, False), - (['#!/usr/bin/python', '# flake8 is great', 'a = 1'], False, False), - (['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], True, False), - (['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], False, True), - (['#!/usr/bin/python', '# flake8:noqa', 'a = 1'], True, False), - (['#!/usr/bin/python', '# flake8:noqa', 'a = 1'], False, True), - (['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], True, False), - (['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], False, True), - (['# flake8:noqa', '#!/usr/bin/python', 'a = 1'], True, False), - (['# flake8:noqa', '#!/usr/bin/python', 'a = 1'], False, True), - (['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], True, False), - (['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], False, True), - (['#!/usr/bin/python', 'a = 1', '# flake8:noqa'], True, False), - (['#!/usr/bin/python', 'a = 1', '# flake8:noqa'], False, True), - (['#!/usr/bin/python', 'a = 1 # flake8: noqa'], False, False), - (['#!/usr/bin/python', 'a = 1 # flake8:noqa'], False, False), +@pytest.mark.parametrize('lines, expected', [ + (['\xEF\xBB\xBF"""Module docstring."""\n'], False), + ([u'\uFEFF"""Module docstring."""\n'], False), + (['#!/usr/bin/python', '# flake8 is great', 'a = 1'], False), + (['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], True), + (['#!/usr/bin/python', '# flake8:noqa', 'a = 1'], True), + (['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], True), + (['# flake8:noqa', '#!/usr/bin/python', 'a = 1'], True), + (['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], True), + (['#!/usr/bin/python', 'a = 1', '# flake8:noqa'], True), + (['#!/usr/bin/python', 'a = 1 # flake8: noqa'], False), + (['#!/usr/bin/python', 'a = 1 # flake8:noqa'], False), ]) -def test_should_ignore_file(lines, expected, disable_noqa, default_options): +def test_should_ignore_file(lines, expected, default_options): """Verify that we ignore a file if told to.""" - default_options.disable_noqa = disable_noqa file_processor = processor.FileProcessor('-', default_options, lines) assert file_processor.should_ignore_file() is expected +@pytest.mark.parametrize('lines', [ + ['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], + ['#!/usr/bin/python', '# flake8:noqa', 'a = 1'], + ['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], + ['# flake8:noqa', '#!/usr/bin/python', 'a = 1'], + ['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], + ['#!/usr/bin/python', 'a = 1', '# flake8:noqa'], +]) +def test_should_ignore_file_to_handle_disable_noqa(lines, default_options): + """Verify that we ignore a file if told to.""" + file_processor = processor.FileProcessor('-', default_options, lines) + assert file_processor.should_ignore_file() is True + default_options.disable_noqa = True + file_processor = processor.FileProcessor('-', default_options, lines) + assert file_processor.should_ignore_file() is False + + @mock.patch('flake8.utils.stdin_get_value') def test_read_lines_from_stdin(stdin_get_value, default_options): """Verify that we use our own utility function to retrieve stdin.""" From 3b80b2e05a075e8de7149035643b2498e5a25c02 Mon Sep 17 00:00:00 2001 From: Isac Yoo Date: Wed, 13 Nov 2019 11:58:49 +0900 Subject: [PATCH 6/6] Remove redundant sample data --- tests/unit/test_file_processor.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/tests/unit/test_file_processor.py b/tests/unit/test_file_processor.py index 9acd5ae..f38568c 100644 --- a/tests/unit/test_file_processor.py +++ b/tests/unit/test_file_processor.py @@ -76,16 +76,9 @@ def test_should_ignore_file(lines, expected, default_options): assert file_processor.should_ignore_file() is expected -@pytest.mark.parametrize('lines', [ - ['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], - ['#!/usr/bin/python', '# flake8:noqa', 'a = 1'], - ['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], - ['# flake8:noqa', '#!/usr/bin/python', 'a = 1'], - ['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], - ['#!/usr/bin/python', 'a = 1', '# flake8:noqa'], -]) -def test_should_ignore_file_to_handle_disable_noqa(lines, default_options): +def test_should_ignore_file_to_handle_disable_noqa(default_options): """Verify that we ignore a file if told to.""" + lines = ['# flake8: noqa'] file_processor = processor.FileProcessor('-', default_options, lines) assert file_processor.should_ignore_file() is True default_options.disable_noqa = True