prospector
prospector copied to clipboard
postfilter.filter_messages uses substring matching on message.code, suppressing unrelated errors
prospector/postfilter.py filters per-line suppressed messages using a substring check (message.code in ignore.code) instead of exact equality (message.code == ignore.code):
for ignore in messages_to_ignore[relative_message_path][message.location.line]:
if (ignore.source is None or message.source == ignore.source) and message.code in ignore.code:
matched = True
continue
Because # noqa: <code> produces an Ignore(None, code) entry with source=None, any message on the same line whose code happens to be a substring of the suppressed code is silently dropped.
Steps to reproduce
from pathlib import Path
import tempfile
from prospector.message import Location, Message
from prospector.postfilter import filter_messages
with tempfile.TemporaryDirectory() as tmpdir:
sample = Path(tmpdir) / "sample.py"
sample.write_text(
"import missing_mod # noqa: unused-import\n"
"return 1 # noqa: no-else-return\n",
encoding="utf-8",
)
loc1 = Location(sample, None, None, 1, 0)
loc2 = Location(sample, None, None, 2, 0)
msgs = [
Message("pylint", "unused-import", loc1, "Unused import"),
Message("mypy", "import", loc1, "Cannot find implementation or library stub"),
Message("pylint", "no-else-return", loc2, "Unnecessary else after return"),
Message("mypy", "return", loc2, "Missing return statement"),
]
print([(m.source, m.code) for m in filter_messages([sample], msgs)])
Expected behavior
[('mypy', 'import'), ('mypy', 'return')] — only the explicitly suppressed codes (unused-import, no-else-return) are filtered out.
Actual behavior
[] — mypy's import and return messages are dropped because "import" in "unused-import" and "return" in "no-else-return" evaluate to True.