Skip to content

Commit

Permalink
Refactor --list-msgs & --list-msgs-enabled (#4793)
Browse files Browse the repository at this point in the history
* Refactor ``--list-msgs`` & ``--list-msgs-enabled``
Both options now show which messages can't be emitted with the
current interpreter. This makes function more like their name implies.
This closes #4778

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
DanielNoord and Pierre-Sassoulas committed Aug 3, 2021
1 parent 741276e commit a71cfe1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Expand Up @@ -75,6 +75,11 @@ Release date: TBA
Closes #3608
Closes #4346


* Refactor of ``--list-msgs`` & ``--list-msgs-enabled``: both options now show whether messages are emittable with the current interpreter.

Closes #4778

* Fix false negative for ``used-before-assignment`` when the variable is assigned
in an exception handler, but used outside of the handler.

Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.10.rst
Expand Up @@ -62,6 +62,10 @@ Other Changes
Closes #3608
Closes #4346

* Refactor of ``--list-msgs`` & ``--list-msgs-enabled``: both options now show whether messages are emittable with the current interpreter.

Closes #4778

* Fix false negative for ``used-before-assignment`` when the variable is assigned
in an exception handler, but used outside of the handler.

Expand Down
25 changes: 13 additions & 12 deletions pylint/lint/pylinter.py
Expand Up @@ -775,22 +775,23 @@ def python3_porting_mode(self):
self._python3_porting_mode = True

def list_messages_enabled(self):
enabled = [
f" {message.symbol} ({message.msgid})"
for message in self.msgs_store.messages
if self.is_message_enabled(message.msgid)
]
disabled = [
f" {message.symbol} ({message.msgid})"
for message in self.msgs_store.messages
if not self.is_message_enabled(message.msgid)
]
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in sorted(enabled):
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in sorted(disabled):
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")

# block level option handling #############################################
Expand Down
7 changes: 4 additions & 3 deletions pylint/lint/run.py
Expand Up @@ -156,7 +156,8 @@ def __init__(
"callback": self.cb_list_messages,
"group": "Commands",
"level": 1,
"help": "Generate pylint's messages.",
"help": "Display a list of all pylint's messages divided by whether "
"they are emittable with the given interpreter.",
},
),
(
Expand All @@ -167,8 +168,8 @@ def __init__(
"callback": self.cb_list_messages_enabled,
"group": "Commands",
"level": 1,
"help": "Display a list of what messages are enabled "
"and disabled with the given configuration.",
"help": "Display a list of what messages are enabled, "
"disabled and non-emittable with the given configuration.",
},
),
(
Expand Down
26 changes: 21 additions & 5 deletions pylint/message/message_definition_store.py
Expand Up @@ -2,7 +2,7 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE

import collections
from typing import Dict, List, ValuesView
from typing import Dict, List, Tuple, ValuesView

from pylint.exceptions import UnknownMessageError
from pylint.message.message_definition import MessageDefinition
Expand Down Expand Up @@ -73,9 +73,25 @@ def help_message(self, msgids_or_symbols: List[str]) -> None:

def list_messages(self) -> None:
"""Output full messages list documentation in ReST format."""
emittable, non_emittable = self.find_emittable_messages()
print("Emittable messages with current interpreter:")
for msg in emittable:
print(msg.format_help(checkerref=False))
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(msg.format_help(checkerref=False))
print("")

def find_emittable_messages(
self,
) -> Tuple[List[MessageDefinition], List[MessageDefinition]]:
"""Finds all emittable and non-emittable messages"""
messages = sorted(self._messages_definitions.values(), key=lambda m: m.msgid)
emittable = []
non_emittable = []
for message in messages:
if not message.may_be_emitted():
continue
print(message.format_help(checkerref=False))
print("")
if message.may_be_emitted():
emittable.append(message)
else:
non_emittable.append(message)
return emittable, non_emittable

0 comments on commit a71cfe1

Please sign in to comment.