Skip to content

Commit f388437

Browse files
[deleted messages] Add an exception for deleted messages in MessageIdStore
1 parent 90d9630 commit f388437

11 files changed

+103
-13
lines changed

pylint/config/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,8 @@ def _enable_all_extensions(run: Run, value: str | None) -> None:
203203
PREPROCESSABLE_OPTIONS: dict[
204204
str, tuple[bool, Callable[[Run, str | None], None], int]
205205
] = { # pylint: disable=consider-using-namedtuple-or-dataclass
206-
# pylint: disable=wrong-spelling-in-comment
207206
# Argparse by default allows abbreviations. It behaves differently
208-
# if you turn this off, so we also turn it on. We mimick this
207+
# if you turn this off, so we also turn it on. We mimic this
209208
# by allowing some abbreviations or incorrect spelling here.
210209
# The integer at the end of the tuple indicates how many letters
211210
# should match, include the '-'. 0 indicates a full match.

pylint/exceptions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ class UnknownMessageError(Exception):
1313
"""Raised when an unregistered message id is encountered."""
1414

1515

16+
class DeletedMessageError(UnknownMessageError):
17+
"""Raised when a message id or symbol that was deleted from pylint is
18+
encountered.
19+
"""
20+
21+
def __init__(self, msgid_or_symbol: str, removal_explanation: str):
22+
super().__init__(
23+
f"'{msgid_or_symbol}' was removed from pylint, see {removal_explanation}."
24+
)
25+
26+
1627
class EmptyReportError(Exception):
1728
"""Raised when a report is empty and so should not be displayed."""
1829

pylint/message/_deleted_message_ids.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import annotations
66

7+
from functools import lru_cache
78
from typing import NamedTuple
89

910

@@ -123,3 +124,23 @@ class DeletedMessage(NamedTuple):
123124
DeletedMessage("W0111", "assign-to-new-keyword"),
124125
],
125126
}
127+
128+
129+
@lru_cache(maxsize=None)
130+
def is_deleted_symbol(symbol: str) -> str | None:
131+
"""Return the explanation for removal if the message was removed."""
132+
for explanation, deleted_messages in DELETED_MESSAGES_IDS.items():
133+
for deleted_message in deleted_messages:
134+
if symbol == deleted_message.symbol:
135+
return explanation
136+
return None
137+
138+
139+
@lru_cache(maxsize=None)
140+
def is_deleted_msgid(msgid: str) -> str | None:
141+
"""Return the explanation for removal if the message was removed."""
142+
for explanation, deleted_messages in DELETED_MESSAGES_IDS.items():
143+
for deleted_message in deleted_messages:
144+
if msgid == deleted_message.msgid:
145+
return explanation
146+
return None

pylint/message/message_id_store.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66

77
from typing import NoReturn
88

9-
from pylint.exceptions import InvalidMessageError, UnknownMessageError
9+
from pylint.exceptions import (
10+
DeletedMessageError,
11+
InvalidMessageError,
12+
UnknownMessageError,
13+
)
14+
from pylint.message._deleted_message_ids import is_deleted_msgid, is_deleted_symbol
1015

1116

1217
class MessageIdStore:
@@ -122,18 +127,24 @@ def get_active_msgids(self, msgid_or_symbol: str) -> list[str]:
122127

123128
# If we don't have a cached value yet we compute it
124129
msgid: str | None
130+
deletion_reason = None
125131
if msgid_or_symbol[1:].isdigit():
126132
# Only msgid can have a digit as second letter
127133
msgid = msgid_or_symbol.upper()
128134
symbol = self.__msgid_to_symbol.get(msgid)
135+
if not symbol:
136+
deletion_reason = is_deleted_msgid(msgid)
129137
else:
130-
msgid = self.__symbol_to_msgid.get(msgid_or_symbol)
131138
symbol = msgid_or_symbol
139+
msgid = self.__symbol_to_msgid.get(msgid_or_symbol)
140+
if not msgid:
141+
deletion_reason = is_deleted_symbol(symbol)
132142
if not msgid or not symbol:
143+
if deletion_reason is not None:
144+
raise DeletedMessageError(msgid_or_symbol, deletion_reason)
133145
error_msg = f"No such message id or symbol '{msgid_or_symbol}'."
134146
raise UnknownMessageError(error_msg)
135147
ids = self.__old_names.get(msgid, [msgid])
136-
137148
# Add to cache
138149
self.__active_msgids[msgid_or_symbol] = ids
139150
return ids

script/get_unused_message_id_category.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pylint.checkers import initialize as initialize_checkers
99
from pylint.extensions import initialize as initialize_extensions
1010
from pylint.lint.pylinter import PyLinter
11-
from pylint.message.deleted_message_ids import _DELETED_MSGID_PREFIXES
11+
from pylint.message._deleted_message_ids import DELETED_MSGID_PREFIXES
1212

1313

1414
def register_all_checkers_and_plugins(linter: PyLinter) -> None:
@@ -21,7 +21,7 @@ def register_all_checkers_and_plugins(linter: PyLinter) -> None:
2121
def get_next_code_category(message_ids: list[str]) -> int:
2222
categories = sorted({int(i[:2]) for i in message_ids})
2323
# We add the prefixes for deleted checkers
24-
categories += _DELETED_MSGID_PREFIXES
24+
categories += DELETED_MSGID_PREFIXES
2525
for i in categories:
2626
if i + 1 not in categories:
2727
return i + 1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
************* Module {abspath}
2+
{relpath}:1:0: E0012: Bad option value for --disable. Don't recognize message buffer-builtin. (bad-option-value)
3+
{relpath}:1:0: E0012: Bad option value for --enable. Don't recognize message useless-option-value. (bad-option-value)
4+
{relpath}:1:0: E0012: Bad option value for --enable. Don't recognize message cmp-builtin. (bad-option-value)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Check that we raise an informational when a deleted messages exists in a .pylintrc file
2+
# See https://github.com/PyCQA/pylint/issues/6794
3+
[messages control]
4+
disable = logging-not-lazy, buffer-builtin
5+
enable = useless-option-value, locally-disabled, cmp-builtin
6+
jobs = 10
7+
reports = yes
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"functional_append": {
3+
"disable": ["logging-not-lazy"],
4+
"enable": ["locally-disabled"]
5+
},
6+
"functional_remove": {
7+
"disable": ["locally-disabled"],
8+
"enable": ["logging-not-lazy"]
9+
},
10+
"jobs": 10,
11+
"reports": true
12+
}
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1+
"""Check unknown or deleted option. """
2+
3+
# Standard disable with unknown value
4+
# pylint: disable=C05048 # [bad-option-value]
5+
# Standard disable with deleted symbol
6+
# pylint: disable=execfile-builtin # [bad-option-value]
7+
# Standard disable with deleted msgid
8+
# pylint: disable=W1656 # [bad-option-value]
9+
# disable-next with unknown value
10+
# pylint: disable-next=R78948 # [bad-option-value]
11+
# disable-next with deleted symbol
12+
# pylint: disable-next=deprecated-types-field # [bad-option-value]
13+
# disable-next with deleted msgid
14+
# pylint: disable-next=W1634 # [bad-option-value]
15+
16+
# enable with unknown value
117
# pylint:enable=W04044 # [bad-option-value]
2-
"""check unknown option
3-
"""
4-
__revision__ = 1
18+
# enable with deleted symbol
19+
# pylint:enable=no-space-after-comma # [bad-option-value]
20+
# enable with deleted msgid
21+
# pylint:enable=W1622 # [bad-option-value]
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
bad-option-value:1:0:None:None::Bad option value for enable. Don't recognize message W04044.:UNDEFINED
1+
bad-option-value:4:0:None:None::Bad option value for disable. Don't recognize message C05048.:UNDEFINED
2+
bad-option-value:6:0:None:None::Bad option value for disable. Don't recognize message execfile-builtin.:UNDEFINED
3+
bad-option-value:8:0:None:None::Bad option value for disable. Don't recognize message W1656.:UNDEFINED
4+
bad-option-value:10:0:None:None::Bad option value for disable-next. Don't recognize message R78948.:UNDEFINED
5+
bad-option-value:12:0:None:None::Bad option value for disable-next. Don't recognize message deprecated-types-field.:UNDEFINED
6+
bad-option-value:14:0:None:None::Bad option value for disable-next. Don't recognize message W1634.:UNDEFINED
7+
bad-option-value:17:0:None:None::Bad option value for enable. Don't recognize message W04044.:UNDEFINED
8+
bad-option-value:19:0:None:None::Bad option value for enable. Don't recognize message no-space-after-comma.:UNDEFINED
9+
bad-option-value:21:0:None:None::Bad option value for enable. Don't recognize message W1622.:UNDEFINED

tests/message/test_no_removed_msgid_or_symbol_used.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
44

55
from pylint.lint import PyLinter
6-
from pylint.message.deleted_message_ids import _DELETED_MESSAGES_IDS
6+
from pylint.message._deleted_message_ids import DELETED_MESSAGES_IDS
77

88

99
def test_no_removed_msgid_or_symbol_used(linter: PyLinter) -> None:
@@ -12,7 +12,7 @@ def test_no_removed_msgid_or_symbol_used(linter: PyLinter) -> None:
1212
This could cause occasional bugs, but more importantly confusion and inconsistencies
1313
when searching for old msgids online. See https://github.com/PyCQA/pylint/issues/5729
1414
"""
15-
for deleted_messages in _DELETED_MESSAGES_IDS.values():
15+
for deleted_messages in DELETED_MESSAGES_IDS.values():
1616
for msgid, symbol, old_names in deleted_messages:
1717
linter.msgs_store.message_id_store.register_message_definition(
1818
msgid, symbol, old_names

0 commit comments

Comments
 (0)