Skip to content

Commit 82ecd77

Browse files
Proof of concept of named message sets
1 parent 4f6a3a9 commit 82ecd77

File tree

8 files changed

+69
-1
lines changed

8 files changed

+69
-1
lines changed

doc/tutorial.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ hint of what Pylint is going to ``pick on``: ::
5757
further processing.
5858

5959
When Pylint is first run on a fresh piece of code, a common complaint is that it
60-
is too ``noisy``. The default configuration enforce a lot of warnings.
60+
is too ``noisy``. The default configuration enforces a lot of warnings.
6161
We'll use some of the options we noted above to make it suit your
6262
preferences a bit better.
6363

pylint/__init__.py

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

77
__all__ = [
88
"__version__",
9+
"core",
910
"modify_sys_path",
11+
"recommended",
1012
"run_pylint",
1113
"run_pyreverse",
1214
"run_symilar",
@@ -19,6 +21,7 @@
1921
from typing import NoReturn
2022

2123
from pylint.__pkginfo__ import __version__
24+
from pylint.message_sets import core, recommended
2225

2326
# pylint: disable=import-outside-toplevel
2427

pylint/config/arguments_manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ def _generate_config(
239239
"""Write a configuration file according to the current configuration
240240
into the given stream or stdout.
241241
"""
242+
# If --message-sets is absent, maybe just go ahead and
243+
# set --message-sets=pylint.recommended? or wait until pylint 5?
242244
options_by_section = {}
243245
sections = []
244246
for group in sorted(

pylint/config/callback_actions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,24 @@ def __call__(
382382
raise NotImplementedError # pragma: no cover
383383

384384

385+
class _MessageSetsAction(_AccessLinterObjectAction):
386+
"""Callback action for setting message sets."""
387+
388+
def __call__(
389+
self,
390+
parser: argparse.ArgumentParser,
391+
namespace: argparse.Namespace,
392+
values: str | Sequence[Any] | None,
393+
option_string: str | None = "--disable",
394+
) -> None:
395+
for msg_set in values or ():
396+
actions = utils.utils._import_attribute(msg_set)
397+
for enable in actions["enable"]:
398+
self.linter.enable(enable)
399+
for disable in actions["disable"]:
400+
self.linter.disable(disable)
401+
402+
385403
class _DisableAction(_XableAction):
386404
"""Callback action for disabling a message."""
387405

pylint/lint/base_options.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
_ListMessagesEnabledAction,
2727
_LongHelpAction,
2828
_MessageHelpAction,
29+
_MessageSetsAction,
2930
_OutputFormatAction,
3031
)
3132
from pylint.typing import Options
@@ -181,6 +182,19 @@ def _make_linter_options(linter: PyLinter) -> Options:
181182
f" Leave empty to show all. Valid levels: {', '.join(interfaces.CONFIDENCE_LEVEL_NAMES)}.",
182183
},
183184
),
185+
(
186+
"message-sets",
187+
{
188+
"action": _MessageSetsAction,
189+
"callback": lambda x1, x2, x3, x4: x1,
190+
"default": ("pylint.core"), # pylint.recommended in v5
191+
"metavar": "<msg ids>",
192+
"short": "msg-sets",
193+
"group": "Messages control",
194+
"help": "",
195+
"kwargs": {"linter": linter},
196+
},
197+
),
184198
(
185199
"enable",
186200
{

pylint/message_sets.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
2+
# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
3+
# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
4+
5+
core: dict[str, set[str]] = {
6+
"enable": set(),
7+
"disable": set(),
8+
}
9+
10+
recommended: dict[str, set[str]] = {
11+
"enable": set(),
12+
"disable": {
13+
"duplicate-code",
14+
# add others from issue...
15+
},
16+
}

pylint/utils/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import warnings
2525
from collections import deque
2626
from collections.abc import Iterable, Sequence
27+
from importlib import import_module
2728
from io import BufferedReader, BytesIO
2829
from re import Pattern
2930
from typing import TYPE_CHECKING, Any, Literal, TextIO, TypeVar
@@ -264,6 +265,18 @@ def _check_regexp_csv(value: list[str] | tuple[str] | str) -> Iterable[str]:
264265
yield from ("".join(regexp).strip() for regexp in regexps if regexp is not None)
265266

266267

268+
def _import_attribute(dotted_path: str) -> Any:
269+
try:
270+
module_path, attribute_name = dotted_path.rsplit(".", 1)
271+
except ValueError as err:
272+
raise ImportError(f"{dotted_path} doesn't look like a module path") from err
273+
274+
if not (module := sys.modules.get(module_path)):
275+
module = import_module(module_path)
276+
277+
return getattr(module, attribute_name)
278+
279+
267280
def _comment(string: str) -> str:
268281
"""Return string as a comment."""
269282
lines = [line.strip() for line in string.splitlines()]

pylintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ clear-cache-post-run=no
7676
# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED
7777
# confidence=
7878

79+
message-sets=pylint.recommended
80+
7981
# Enable the message, report, category or checker with the given id(s). You can
8082
# either give multiple identifier separated by comma (,) or put this option
8183
# multiple time (only on the command line, not in the configuration file where

0 commit comments

Comments
 (0)