Skip to content

Commit bb4a567

Browse files
Fixes custom braces in user-provided msg-template. (#7976) (#7993)
Closes #5636 Co-authored-by: Pierre Sassoulas <[email protected]> Co-authored-by: Dani Alcala <[email protected]>
1 parent e907020 commit bb4a567

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

doc/whatsnew/fragments/5636.bugfix

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Using custom braces in ``msg-template`` will now work properly.
2+
3+
Closes #5636

pylint/reporters/text.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def on_set_current_module(self, module: str, filepath: str | None) -> None:
175175
self._template = template
176176

177177
# Check to see if all parameters in the template are attributes of the Message
178-
arguments = re.findall(r"\{(.+?)(:.*)?\}", template)
178+
arguments = re.findall(r"\{(\w+?)(:.*)?\}", template)
179179
for argument in arguments:
180180
if argument[0] not in MESSAGE_FIELDS:
181181
warnings.warn(

tests/reporters/unittest_reporting.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing import TYPE_CHECKING
1515

1616
import pytest
17+
from _pytest.recwarn import WarningsRecorder
1718

1819
from pylint import checkers
1920
from pylint.interfaces import HIGH
@@ -88,16 +89,12 @@ def test_template_option_non_existing(linter) -> None:
8889
"""
8990
output = StringIO()
9091
linter.reporter.out = output
91-
linter.config.msg_template = (
92-
"{path}:{line}:{a_new_option}:({a_second_new_option:03d})"
93-
)
92+
linter.config.msg_template = "{path}:{line}:{categ}:({a_second_new_option:03d})"
9493
linter.open()
9594
with pytest.warns(UserWarning) as records:
9695
linter.set_current_module("my_mod")
9796
assert len(records) == 2
98-
assert (
99-
"Don't recognize the argument 'a_new_option'" in records[0].message.args[0]
100-
)
97+
assert "Don't recognize the argument 'categ'" in records[0].message.args[0]
10198
assert (
10299
"Don't recognize the argument 'a_second_new_option'"
103100
in records[1].message.args[0]
@@ -113,7 +110,24 @@ def test_template_option_non_existing(linter) -> None:
113110
assert out_lines[2] == "my_mod:2::()"
114111

115112

116-
def test_deprecation_set_output(recwarn):
113+
def test_template_option_with_header(linter: PyLinter) -> None:
114+
output = StringIO()
115+
linter.reporter.out = output
116+
linter.config.msg_template = '{{ "Category": "{category}" }}'
117+
linter.open()
118+
linter.set_current_module("my_mod")
119+
120+
linter.add_message("C0301", line=1, args=(1, 2))
121+
linter.add_message(
122+
"line-too-long", line=2, end_lineno=2, end_col_offset=4, args=(3, 4)
123+
)
124+
125+
out_lines = output.getvalue().split("\n")
126+
assert out_lines[1] == '{ "Category": "convention" }'
127+
assert out_lines[2] == '{ "Category": "convention" }'
128+
129+
130+
def test_deprecation_set_output(recwarn: WarningsRecorder) -> None:
117131
"""TODO remove in 3.0."""
118132
reporter = BaseReporter()
119133
# noinspection PyDeprecation

0 commit comments

Comments
 (0)