diff --git a/doc/whatsnew/fragments/5636.bugfix b/doc/whatsnew/fragments/5636.bugfix new file mode 100644 index 0000000000..efa6d5452d --- /dev/null +++ b/doc/whatsnew/fragments/5636.bugfix @@ -0,0 +1,3 @@ +Using custom braces in ``msg-template`` will now work properly. + +Closes #5636 diff --git a/pylint/reporters/text.py b/pylint/reporters/text.py index 29bd46798f..e8584e3b30 100644 --- a/pylint/reporters/text.py +++ b/pylint/reporters/text.py @@ -175,7 +175,7 @@ def on_set_current_module(self, module: str, filepath: str | None) -> None: self._template = template # Check to see if all parameters in the template are attributes of the Message - arguments = re.findall(r"\{(.+?)(:.*)?\}", template) + arguments = re.findall(r"\{(\w+?)(:.*)?\}", template) for argument in arguments: if argument[0] not in MESSAGE_FIELDS: warnings.warn( diff --git a/tests/reporters/unittest_reporting.py b/tests/reporters/unittest_reporting.py index 37f3e5fd97..f69be520dc 100644 --- a/tests/reporters/unittest_reporting.py +++ b/tests/reporters/unittest_reporting.py @@ -14,6 +14,7 @@ from typing import TYPE_CHECKING import pytest +from _pytest.recwarn import WarningsRecorder from pylint import checkers from pylint.interfaces import HIGH @@ -88,16 +89,12 @@ def test_template_option_non_existing(linter) -> None: """ output = StringIO() linter.reporter.out = output - linter.config.msg_template = ( - "{path}:{line}:{a_new_option}:({a_second_new_option:03d})" - ) + linter.config.msg_template = "{path}:{line}:{categ}:({a_second_new_option:03d})" linter.open() with pytest.warns(UserWarning) as records: linter.set_current_module("my_mod") assert len(records) == 2 - assert ( - "Don't recognize the argument 'a_new_option'" in records[0].message.args[0] - ) + assert "Don't recognize the argument 'categ'" in records[0].message.args[0] assert ( "Don't recognize the argument 'a_second_new_option'" in records[1].message.args[0] @@ -113,7 +110,24 @@ def test_template_option_non_existing(linter) -> None: assert out_lines[2] == "my_mod:2::()" -def test_deprecation_set_output(recwarn): +def test_template_option_with_header(linter: PyLinter) -> None: + output = StringIO() + linter.reporter.out = output + linter.config.msg_template = '{{ "Category": "{category}" }}' + linter.open() + linter.set_current_module("my_mod") + + linter.add_message("C0301", line=1, args=(1, 2)) + linter.add_message( + "line-too-long", line=2, end_lineno=2, end_col_offset=4, args=(3, 4) + ) + + out_lines = output.getvalue().split("\n") + assert out_lines[1] == '{ "Category": "convention" }' + assert out_lines[2] == '{ "Category": "convention" }' + + +def test_deprecation_set_output(recwarn: WarningsRecorder) -> None: """TODO remove in 3.0.""" reporter = BaseReporter() # noinspection PyDeprecation