Skip to content

Commit f33b822

Browse files
authored
Add GitHub reporter (#9484)
* Add GitHub reporter A new `github` reporter has been added. This reporter automatically annotates code on GitHub's UI with the messages found during linting. Closes #9443. * Enable github output format for CI * Apply corrections Used message.C directly instead of slicing again, improved feature notes and removed unecessary output from locally run pre-commit pylint hook. It was kept for the manual stage since that is run on Github and needs it to annotate code on PRs.
1 parent 0d120a5 commit f33b822

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

.pre-commit-config.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,15 @@ repos:
8787
entry: pylint
8888
language: system
8989
types: [python]
90-
args: ["-rn", "-sn", "--rcfile=pylintrc", "--fail-on=I", "--spelling-dict=en"]
90+
args:
91+
[
92+
"-rn",
93+
"-sn",
94+
"--rcfile=pylintrc",
95+
"--fail-on=I",
96+
"--spelling-dict=en",
97+
"--output-format=github",
98+
]
9199
exclude: tests(/\w*)*/functional/|tests/input|tests(/\w*)*data/|doc/
92100
stages: [manual]
93101
- id: sphinx-generated-doc

doc/whatsnew/fragments/9443.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A new `github` reporter has been added. This reporter returns the output of `pylint` in a format that
2+
Github can use to automatically annotate code. Use it with `pylint --output-format=github` on your Github Workflows.
3+
4+
Closes #9443.

pylint/reporters/text.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,35 @@ def handle_message(self, msg: Message) -> None:
255255
self.write_message(msg)
256256

257257

258+
class GithubReporter(TextReporter):
259+
"""Report messages in GitHub's special format to annotate code in its user
260+
interface.
261+
"""
262+
263+
name = "github"
264+
line_format = "::{category} file={path},line={line},endline={end_line},col={column},title={msg_id}::{msg}"
265+
category_map = {
266+
"F": "error",
267+
"E": "error",
268+
"W": "warning",
269+
"C": "notice",
270+
"R": "notice",
271+
"I": "notice",
272+
}
273+
274+
def write_message(self, msg: Message) -> None:
275+
self_dict = asdict(msg)
276+
for key in ("end_line", "end_column"):
277+
self_dict[key] = self_dict[key] or ""
278+
279+
self_dict["category"] = self.category_map.get(msg.C) or "error"
280+
self.writeln(self._fixed_template.format(**self_dict))
281+
282+
258283
def register(linter: PyLinter) -> None:
259284
linter.register_reporter(TextReporter)
260285
linter.register_reporter(NoHeaderReporter)
261286
linter.register_reporter(ParseableTextReporter)
262287
linter.register_reporter(VSTextReporter)
263288
linter.register_reporter(ColorizedTextReporter)
289+
linter.register_reporter(GithubReporter)

tests/test_self.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from pylint.message import Message
3434
from pylint.reporters import BaseReporter
3535
from pylint.reporters.json_reporter import JSON2Reporter
36-
from pylint.reporters.text import ColorizedTextReporter, TextReporter
36+
from pylint.reporters.text import ColorizedTextReporter, GithubReporter, TextReporter
3737
from pylint.testutils._run import _add_rcfile_default_pylintrc
3838
from pylint.testutils._run import _Run as Run
3939
from pylint.testutils.utils import (
@@ -189,6 +189,7 @@ def test_all(self) -> None:
189189
TextReporter(StringIO()),
190190
ColorizedTextReporter(StringIO()),
191191
JSON2Reporter(StringIO()),
192+
GithubReporter(StringIO()),
192193
]
193194
self._runtest(
194195
[join(HERE, "functional", "a", "arguments.py")],

0 commit comments

Comments
 (0)