Skip to content

Commit

Permalink
Fixes custom braces in user-provided msg-template. (#7976) (#7993)
Browse files Browse the repository at this point in the history
Closes #5636

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Co-authored-by: Dani Alcala <112832187+clavedeluna@users.noreply.github.com>
  • Loading branch information
Pierre-Sassoulas and clavedeluna authored Dec 27, 2022
1 parent e907020 commit bb4a567
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/5636.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Using custom braces in ``msg-template`` will now work properly.

Closes #5636
2 changes: 1 addition & 1 deletion pylint/reporters/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
28 changes: 21 additions & 7 deletions tests/reporters/unittest_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down

0 comments on commit bb4a567

Please sign in to comment.