Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default_enabled option to optional message dict #7629

Merged
merged 3 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/exts/pylint_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class MessageData(NamedTuple):
checker_module_name: str
checker_module_path: str
shared: bool = False
default_enabled: bool = True


MessagesDict = Dict[str, List[MessageData]]
Expand Down Expand Up @@ -194,6 +195,7 @@ def _get_all_messages(
checker_module.__name__,
checker_module.__file__,
message.shared,
message.default_enabled,
)
msg_type = MSG_TYPES_DOC[message.msgid[0]]
messages_dict[msg_type].append(message_data)
Expand Down Expand Up @@ -271,7 +273,15 @@ def _generate_single_message_body(message: MessageData) -> str:
**Description:**

*{message.definition.description}*
"""
if not message.default_enabled:
body += f"""
.. caution::
This message is disabled by default. To enable it, add ``{message.name}`` to the ``enable`` option.

"""

body += f"""
{message.bad_code}
{message.good_code}
{message.details}
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/7629.other
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Add ``default_enabled`` option to optional message dict. Provides an option to disable a checker message by default.
To use a disabled message, the user must enable it explicitly by adding the message to the ``enable`` option.

Refs #7629
5 changes: 3 additions & 2 deletions pylint/extensions/code_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class CodeStyleChecker(BaseChecker):
"Emitted when an assignment is referring to the object that it is assigning "
"to. This can be changed to be an augmented assign.\n"
"Disabled by default!",
{
"default_enabled": False,
},
),
}
options = (
Expand Down Expand Up @@ -325,5 +328,3 @@ def visit_assign(self, node: nodes.Assign) -> None:

def register(linter: PyLinter) -> None:
linter.register_checker(CodeStyleChecker(linter))
# Disable some checks by default
linter.disable("consider-using-augmented-assign")
3 changes: 3 additions & 0 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@ def register_checker(self, checker: checkers.BaseChecker) -> None:
self.register_report(r_id, r_title, r_cb, checker)
if hasattr(checker, "msgs"):
self.msgs_store.register_messages_from_checker(checker)
for message in checker.messages:
if not message.default_enabled:
self.disable(message.msgid)
# Register the checker, but disable all of its messages.
if not getattr(checker, "enabled", True):
self.disable(checker.name)
Expand Down
2 changes: 2 additions & 0 deletions pylint/message/message_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
maxversion: tuple[int, int] | None = None,
old_names: list[tuple[str, str]] | None = None,
shared: bool = False,
default_enabled: bool = True,
) -> None:
self.checker_name = checker.name
self.check_msgid(msgid)
Expand All @@ -43,6 +44,7 @@ def __init__(
self.minversion = minversion
self.maxversion = maxversion
self.shared = shared
self.default_enabled = default_enabled
self.old_names: list[tuple[str, str]] = []
if old_names:
for old_msgid, old_symbol in old_names:
Expand Down
1 change: 1 addition & 0 deletions pylint/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class ExtraMessageOptions(TypedDict, total=False):
maxversion: tuple[int, int]
minversion: tuple[int, int]
shared: bool
default_enabled: bool


MessageDefinitionTuple = Union[
Expand Down