Skip to content

Commit ef031be

Browse files
[primer] Rewrite standalone useless-suppression as removed message
When a useless-suppression appears without a paired locally-disabled in the new messages, it means the suppressed checker no longer fires. Parse the symbol from the message text and present it as a removed message instead. TODO: the useless_suppression test case may not accurately represent the real primer scenario (astropy uses `# pylint: disable=W,C,R`).
1 parent 0ab318a commit ef031be

4 files changed

Lines changed: 61 additions & 8 deletions

File tree

pylint/testutils/_primer/comparator.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import annotations
66

77
import json
8+
import re
89
from collections import defaultdict
910
from collections.abc import Iterator
1011
from difflib import SequenceMatcher
@@ -16,6 +17,7 @@
1617
ChangedMessage = tuple[OldJsonExport, OldJsonExport] # (old, new)
1718

1819
_LOCATION_KEYS = {"line", "column", "endLine", "endColumn"}
20+
_USELESS_SUPPRESSION_RE = re.compile(r"Useless suppression of '(.+)'")
1921

2022

2123
def _position_key(msg: OldJsonExport) -> tuple[str, str, str]:
@@ -28,6 +30,45 @@ def _position_key(msg: OldJsonExport) -> tuple[str, str, str]:
2830
return (msg["symbol"], msg["path"], msg["obj"])
2931

3032

33+
def _rewrite_useless_suppressions(
34+
new_messages: list[OldJsonExport],
35+
missing_messages: list[OldJsonExport],
36+
) -> None:
37+
"""Convert standalone ``useless-suppression`` into removed messages.
38+
39+
A standalone ``useless-suppression`` (no paired ``locally-disabled`` at the
40+
same location in *new_messages*) means the suppressed checker no longer
41+
fires — i.e. the underlying message was effectively removed. We parse the
42+
symbol from the message text and move it to *missing_messages*.
43+
"""
44+
locally_disabled_locations: set[tuple[str, int]] = {
45+
(msg["path"], msg["line"])
46+
for msg in new_messages
47+
if msg["symbol"] == "locally-disabled"
48+
}
49+
to_remove: list[OldJsonExport] = []
50+
for msg in new_messages:
51+
if msg["symbol"] != "useless-suppression":
52+
continue
53+
if (msg["path"], msg["line"]) in locally_disabled_locations:
54+
continue # Paired with a new locally-disabled — keep both as-is.
55+
match = _USELESS_SUPPRESSION_RE.match(msg["message"])
56+
if not match:
57+
continue
58+
suppressed_symbol = match.group(1)
59+
to_remove.append(msg)
60+
missing_messages.append(
61+
{
62+
**msg, # type: ignore[typeddict-item]
63+
"symbol": suppressed_symbol,
64+
"message": f"'{suppressed_symbol}' would have been emitted if it wasn't already user disabled",
65+
"message-id": "",
66+
}
67+
)
68+
for msg in to_remove:
69+
new_messages.remove(msg)
70+
71+
3172
def _match_residuals(
3273
old_residuals: list[OldJsonExport], new_residuals: list[OldJsonExport]
3374
) -> tuple[list[ChangedMessage], list[OldJsonExport], list[OldJsonExport]]:
@@ -156,6 +197,7 @@ def __init__(self, main_data: PackageMessages, pr_data: PackageMessages) -> None
156197
residual_old, pr_data[package]["messages"]
157198
)
158199

200+
_rewrite_useless_suppressions(final_new, final_missing)
159201
self.missing_messages[package] = final_missing
160202
self.new_messages[package] = final_new
161203
self.changed_messages[package] = paired

tests/testutils/_primer/cases/useless_suppression/expected.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33

44
**Effect on [astropy](https://github.com/astropy/astropy):**
55

6-
The following messages are now emitted:
6+
The following messages are no longer emitted:
77

88
<details>
99

10-
1) locally-disabled:
11-
*Locally disabling looping-through-iterator (W4801)*
12-
https://github.com/astropy/astropy/blob/1fb40bc1f22f176254ef583065aa155f53f3b414/astropy/coordinates/angles/angle_parsetab.py#L14
13-
2) useless-suppression:
14-
*Useless suppression of 'looping-through-iterator'*
10+
1) looping-through-iterator:
11+
*'looping-through-iterator' would have been emitted if it wasn't already user disabled*
1512
https://github.com/astropy/astropy/blob/1fb40bc1f22f176254ef583065aa155f53f3b414/astropy/coordinates/angles/angle_parsetab.py#L14
1613
</details>
1714

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{ "package": "astropy", "missing": 0, "new": 2, "changed": 0 }]
1+
[{ "package": "astropy", "missing": 1, "new": 0, "changed": 0 }]
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
{
22
"astropy": {
33
"commit": "1fb40bc1f22f176254ef583065aa155f53f3b414",
4-
"messages": []
4+
"messages": [
5+
{
6+
"type": "info",
7+
"module": "astropy.coordinates.angles.angle_parsetab",
8+
"obj": "",
9+
"line": 14,
10+
"column": 0,
11+
"endLine": null,
12+
"endColumn": null,
13+
"path": "tests/.pylint_primer_tests/astropy/astropy/astropy/coordinates/angles/angle_parsetab.py",
14+
"symbol": "locally-disabled",
15+
"message": "Locally disabling looping-through-iterator (W4801)",
16+
"message-id": "I0011"
17+
}
18+
]
519
}
620
}

0 commit comments

Comments
 (0)