Skip to content

Commit 2b3d113

Browse files
[colorize_ansi] Remove the possibility to use anything else than a MessageStyle (#8412)
1 parent 6789fad commit 2b3d113

File tree

2 files changed

+35
-80
lines changed

2 files changed

+35
-80
lines changed

doc/whatsnew/fragments/8412.breaking

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``colorize_ansi`` now only accept a ``MessageStyle`` object.
2+
3+
Refs #8412

pylint/reporters/text.py

+32-80
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import sys
1616
import warnings
1717
from dataclasses import asdict, fields
18-
from typing import TYPE_CHECKING, Dict, NamedTuple, Optional, TextIO, cast, overload
18+
from typing import TYPE_CHECKING, Dict, NamedTuple, Optional, TextIO, cast
1919

2020
from pylint.message import Message
2121
from pylint.reporters import BaseReporter
@@ -37,6 +37,34 @@ class MessageStyle(NamedTuple):
3737
style: tuple[str, ...] = ()
3838
"""Tuple of style strings (see `ANSI_COLORS` for available values)."""
3939

40+
def __get_ansi_code(self) -> str:
41+
"""Return ANSI escape code corresponding to color and style.
42+
43+
:raise KeyError: if a nonexistent color or style identifier is given
44+
45+
:return: the built escape code
46+
"""
47+
ansi_code = [ANSI_STYLES[effect] for effect in self.style]
48+
if self.color:
49+
if self.color.isdigit():
50+
ansi_code.extend(["38", "5"])
51+
ansi_code.append(self.color)
52+
else:
53+
ansi_code.append(ANSI_COLORS[self.color])
54+
if ansi_code:
55+
return ANSI_PREFIX + ";".join(ansi_code) + ANSI_END
56+
return ""
57+
58+
def _colorize_ansi(self, msg: str) -> str:
59+
if self.color is None and len(self.style) == 0:
60+
# If both color and style are not defined, then leave the text as is.
61+
return msg
62+
escape_code = self.__get_ansi_code()
63+
# If invalid (or unknown) color, don't wrap msg with ANSI codes
64+
if escape_code:
65+
return f"{escape_code}{msg}{ANSI_RESET}"
66+
return msg
67+
4068

4169
ColorMappingDict = Dict[str, MessageStyle]
4270

@@ -70,85 +98,9 @@ class MessageStyle(NamedTuple):
7098
"""All fields of the Message class."""
7199

72100

73-
def _get_ansi_code(msg_style: MessageStyle) -> str:
74-
"""Return ANSI escape code corresponding to color and style.
75-
76-
:param msg_style: the message style
77-
78-
:raise KeyError: if a nonexistent color or style identifier is given
79-
80-
:return: the built escape code
81-
"""
82-
ansi_code = [ANSI_STYLES[effect] for effect in msg_style.style]
83-
if msg_style.color:
84-
if msg_style.color.isdigit():
85-
ansi_code.extend(["38", "5"])
86-
ansi_code.append(msg_style.color)
87-
else:
88-
ansi_code.append(ANSI_COLORS[msg_style.color])
89-
if ansi_code:
90-
return ANSI_PREFIX + ";".join(ansi_code) + ANSI_END
91-
return ""
92-
93-
94-
@overload
95-
def colorize_ansi(
96-
msg: str,
97-
msg_style: MessageStyle | None = ...,
98-
) -> str:
99-
...
100-
101-
102-
@overload
103-
def colorize_ansi(
104-
msg: str,
105-
msg_style: str | None = ...,
106-
style: str = ...,
107-
*,
108-
color: str | None = ...,
109-
) -> str:
110-
# Remove for pylint 3.0
111-
...
112-
113-
114-
def colorize_ansi(
115-
msg: str,
116-
msg_style: MessageStyle | str | None = None,
117-
style: str = "",
118-
**kwargs: str | None,
119-
) -> str:
120-
r"""colorize message by wrapping it with ANSI escape codes
121-
122-
:param msg: the message string to colorize
123-
124-
:param msg_style: the message style
125-
or color (for backwards compatibility): the color of the message style
126-
127-
:param style: the message's style elements, this will be deprecated
128-
129-
:param \**kwargs: used to accept `color` parameter while it is being deprecated
130-
131-
:return: the ANSI escaped string
132-
"""
133-
# TODO: 3.0: Remove deprecated typing and only accept MessageStyle as parameter
134-
if not isinstance(msg_style, MessageStyle):
135-
warnings.warn(
136-
"In pylint 3.0, the colorize_ansi function of Text reporters will only accept a "
137-
"MessageStyle parameter",
138-
DeprecationWarning,
139-
stacklevel=2,
140-
)
141-
color = kwargs.get("color")
142-
style_attrs = tuple(_splitstrip(style))
143-
msg_style = MessageStyle(color or msg_style, style_attrs)
144-
# If both color and style are not defined, then leave the text as is
145-
if msg_style.color is None and len(msg_style.style) == 0:
146-
return msg
147-
escape_code = _get_ansi_code(msg_style)
148-
# If invalid (or unknown) color, don't wrap msg with ANSI codes
149-
if escape_code:
150-
return f"{escape_code}{msg}{ANSI_RESET}"
151-
return msg
101+
def colorize_ansi(msg: str, msg_style: MessageStyle) -> str:
102+
"""Colorize message by wrapping it with ANSI escape codes."""
103+
return msg_style._colorize_ansi(msg)
152104

153105

154106
def make_header(msg: Message) -> str:

0 commit comments

Comments
 (0)