Skip to content

Commit

Permalink
Merge pull request #15927 from ccordoba12/issue-11033
Browse files Browse the repository at this point in the history
PR: Restore ability to ignore linting messages with inline comments (Editor)
  • Loading branch information
ccordoba12 committed Jun 23, 2021
2 parents 698e102 + 02394cd commit 277312b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
14 changes: 13 additions & 1 deletion spyder/plugins/editor/widgets/codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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)

Expand Down
39 changes: 39 additions & 0 deletions spyder/plugins/editor/widgets/tests/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 277312b

Please sign in to comment.