Skip to content

Commit 690e59a

Browse files
[colorize_ansi] Remove the possibility to use anything else than a MessageStyle
1 parent b2ab82a commit 690e59a

File tree

2 files changed

+26
-74
lines changed

2 files changed

+26
-74
lines changed

doc/whatsnew/fragments/8412.breaking

Lines changed: 3 additions & 0 deletions
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

Lines changed: 23 additions & 74 deletions
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,24 @@ 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+
4058

4159
ColorMappingDict = Dict[str, MessageStyle]
4260

@@ -70,81 +88,12 @@ class MessageStyle(NamedTuple):
7088
"""All fields of the Message class."""
7189

7290

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
91+
def colorize_ansi(msg: str, msg_style: MessageStyle) -> str:
92+
"""Colorize message by wrapping it with ANSI escape codes."""
14593
if msg_style.color is None and len(msg_style.style) == 0:
94+
# If both color and style are not defined, then leave the text as is.
14695
return msg
147-
escape_code = _get_ansi_code(msg_style)
96+
escape_code = msg_style.get_ansi_code()
14897
# If invalid (or unknown) color, don't wrap msg with ANSI codes
14998
if escape_code:
15099
return f"{escape_code}{msg}{ANSI_RESET}"

0 commit comments

Comments
 (0)