Skip to content

Commit 03bd452

Browse files
Fixes custom braces in user-provided msg-template. (#7976)
Closes #5636 Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 3a257f5 commit 03bd452

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

doc/whatsnew/fragments/5636.bugfix

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def on_set_current_module(self, module: str, filepath: str | None) -> None:
176176
self._template = template
177177

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

tests/reporters/unittest_reporting.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,12 @@ def test_template_option_non_existing(linter: PyLinter) -> None:
9090
"""
9191
output = StringIO()
9292
linter.reporter.out = output
93-
linter.config.msg_template = (
94-
"{path}:{line}:{a_new_option}:({a_second_new_option:03d})"
95-
)
93+
linter.config.msg_template = "{path}:{line}:{categ}:({a_second_new_option:03d})"
9694
linter.open()
9795
with pytest.warns(UserWarning) as records:
9896
linter.set_current_module("my_mod")
9997
assert len(records) == 2
100-
assert (
101-
"Don't recognize the argument 'a_new_option'" in records[0].message.args[0]
102-
)
98+
assert "Don't recognize the argument 'categ'" in records[0].message.args[0]
10399
assert (
104100
"Don't recognize the argument 'a_second_new_option'"
105101
in records[1].message.args[0]
@@ -115,6 +111,23 @@ def test_template_option_non_existing(linter: PyLinter) -> None:
115111
assert out_lines[2] == "my_mod:2::()"
116112

117113

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

0 commit comments

Comments
 (0)