Skip to content

Commit 85c8f50

Browse files
authored
Colorize "fail-on" messages/categories in ColorizedTextReporter red inverse (#10444)
1 parent 8d4c687 commit 85c8f50

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
colorized reporter now colorizes messages/categories that have been configured as `fail-on` in red inverse.
2+
This makes it easier to quickly find the errors that are causing pylint CI job failures.
3+
4+
Closes #9898

pylint/config/config_initialization.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ def _config_initialization( # pylint: disable=too-many-statements
139139
# enable them
140140
linter.enable_fail_on_messages()
141141

142+
# Now that fail_on messages are enabled, pass them to colorized reporter
143+
linter.pass_fail_on_config_to_color_reporter()
144+
142145
linter._parse_error_mode()
143146

144147
# Link the base Namespace object on the current directory

pylint/lint/pylinter.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
from pylint.message import Message, MessageDefinition, MessageDefinitionStore
5656
from pylint.reporters.base_reporter import BaseReporter
5757
from pylint.reporters.progress_reporters import ProgressReporter
58-
from pylint.reporters.text import TextReporter
58+
from pylint.reporters.text import ColorizedTextReporter, TextReporter
5959
from pylint.reporters.ureports import nodes as report_nodes
6060
from pylint.typing import (
6161
DirectoryNamespaceDict,
@@ -531,6 +531,15 @@ def enable_fail_on_messages(self) -> None:
531531
def any_fail_on_issues(self) -> bool:
532532
return any(x in self.fail_on_symbols for x in self.stats.by_msg.keys())
533533

534+
def pass_fail_on_config_to_color_reporter(self) -> None:
535+
"""Pass fail_on symbol configuration to colorized text reporter."""
536+
if isinstance(self.reporter, ColorizedTextReporter):
537+
self.reporter.set_fail_on_symbols(self.fail_on_symbols)
538+
elif isinstance(self.reporter, reporters.MultiReporter):
539+
for _reporter in self.reporter._sub_reporters:
540+
if isinstance(self.reporter, ColorizedTextReporter):
541+
self.reporter.set_fail_on_symbols(self.fail_on_symbols)
542+
534543
def disable_reporters(self) -> None:
535544
"""Disable all reporters."""
536545
for _reporters in self._reports.values():

pylint/reporters/text.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ class ColorizedTextReporter(TextReporter):
215215
"W": MessageStyle("magenta"),
216216
"E": MessageStyle("red", ("bold",)),
217217
"F": MessageStyle("red", ("bold", "underline")),
218+
"X": MessageStyle("red", ("bold", "inverse")),
218219
"S": MessageStyle("yellow", ("inverse",)), # S stands for module Separator
219220
}
220221

@@ -225,6 +226,7 @@ def __init__(
225226
) -> None:
226227
super().__init__(output)
227228
self.color_mapping = color_mapping or ColorizedTextReporter.COLOR_MAPPING
229+
self.fail_on_symbols: list[str] = []
228230
ansi_terms = ["xterm-16color", "xterm-256color"]
229231
if os.environ.get("TERM") not in ansi_terms:
230232
if sys.platform == "win32":
@@ -237,6 +239,10 @@ def _get_decoration(self, msg_id: str) -> MessageStyle:
237239
"""Returns the message style as defined in self.color_mapping."""
238240
return self.color_mapping.get(msg_id[0]) or MessageStyle(None)
239241

242+
def set_fail_on_symbols(self, fail_on_symbols: list[str]) -> None:
243+
"""Sets the list of configured fail-on symbols."""
244+
self.fail_on_symbols = fail_on_symbols
245+
240246
def handle_message(self, msg: Message) -> None:
241247
"""Manage message of different types, and colorize output
242248
using ANSI escape codes.
@@ -246,7 +252,10 @@ def handle_message(self, msg: Message) -> None:
246252
modsep = colorize_ansi(make_header(msg), msg_style)
247253
self.writeln(modsep)
248254
self._modules.add(msg.module)
249-
msg_style = self._get_decoration(msg.C)
255+
if msg.symbol in self.fail_on_symbols:
256+
msg_style = self._get_decoration("X")
257+
else:
258+
msg_style = self._get_decoration(msg.C)
250259

251260
msg.msg = colorize_ansi(msg.msg, msg_style)
252261
msg.symbol = colorize_ansi(msg.symbol, msg_style)

0 commit comments

Comments
 (0)