From 8621cc432f0d5e4db54884b26ff4b2bb18d65663 Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Wed, 23 Jun 2021 14:57:30 -0500 Subject: [PATCH 1/2] Editor: Restore ability to ignore linting messages according to comments --- spyder/plugins/editor/widgets/codeeditor.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/spyder/plugins/editor/widgets/codeeditor.py b/spyder/plugins/editor/widgets/codeeditor.py index 2a5e27fc922..66ab1c83db2 100644 --- a/spyder/plugins/editor/widgets/codeeditor.py +++ b/spyder/plugins/editor/widgets/codeeditor.py @@ -95,9 +95,10 @@ logger = logging.getLogger(__name__) +# Regexp to detect noqa inline comments. +NOQA_INLINE_REGEXP = re.compile(r"#?noqa", re.IGNORECASE) -# %% This line is for cell execution testing @class_register class CodeEditor(TextEditBaseWidget): """Source Code Editor Widget based exclusively on Qt""" @@ -1260,6 +1261,17 @@ def _process_code_analysis(self, underline): block = document.findBlockByNumber(start['line']) data = block.userData() + + # Skip messages according to certain criteria. + # This one works for any programming language + if 'analysis:ignore' in block.text(): + continue + + # This only works for Python. + if self.language == 'Python': + if NOQA_INLINE_REGEXP.search(block.text()) is not None: + continue + if not data: data = BlockUserData(self) From 02394cdb05e10d47dc5b12188a1b87a2723f414d Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Wed, 23 Jun 2021 15:38:29 -0500 Subject: [PATCH 2/2] Testing: Add test for ignoring linting messages with inline comments --- .../editor/widgets/tests/test_warnings.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/spyder/plugins/editor/widgets/tests/test_warnings.py b/spyder/plugins/editor/widgets/tests/test_warnings.py index 4e59fd193aa..53fc8d53183 100644 --- a/spyder/plugins/editor/widgets/tests/test_warnings.py +++ b/spyder/plugins/editor/widgets/tests/test_warnings.py @@ -329,3 +329,42 @@ def test_update_warnings_after_closebrackets(qtbot, completions_codeeditor_linti qtbot.wait(2000) expected = [['D100: Missing docstring in public module', 1]] assert editor.get_current_warnings() == expected + + +@pytest.mark.slow +@pytest.mark.order(2) +@flaky(max_runs=5) +@pytest.mark.parametrize( + 'ignore_comment', ['#noqa', '# NOQA', '# analysis:ignore', '# no-work'] +) +def test_ignore_warnings_with_comments( + qtbot, completions_codeeditor_linting, ignore_comment +): + """ + Test that ignoring linting messages with inline comments works as + expected. + """ + editor, _ = completions_codeeditor_linting + editor.textCursor().insertText( + f"foo {ignore_comment}\n" + "bar\n" + ) + + if ignore_comment == '# no-work': + expected = [ + ["undefined name 'foo'", 1], + ['D100: Missing docstring in public module', 1], + ['E261 at least two spaces before inline comment', 1], + ["undefined name 'bar'", 2] + ] + else: + expected = [["undefined name 'bar'", 2]] + + # Notify changes. + with qtbot.waitSignal(editor.completions_response_signal, timeout=30000): + editor.document_did_change() + + # Wait for linting info to arrive + qtbot.wait(2000) + + assert editor.get_current_warnings() == expected