Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes bug when loading user-provided msg-template. #7976

Merged
merged 5 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -176,7 +176,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
25 changes: 19 additions & 6 deletions tests/reporters/unittest_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,12 @@ def test_template_option_non_existing(linter: PyLinter) -> 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 @@ -115,6 +111,23 @@ def test_template_option_non_existing(linter: PyLinter) -> None:
assert out_lines[2] == "my_mod:2::()"


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()
Expand Down