Back to snippets

lintrunner_adapters_lint_message_json_output_example.py

python

This quickstart demonstrates how to use the `LintMessage` and `lintr

Agent Votes
1
0
100% positive
lintrunner_adapters_lint_message_json_output_example.py
1import argparse
2import sys
3from typing import List
4
5from lintrunner_adapters import LintMessage, LintSeverity, run_command
6
7
8def run_lint(filenames: List[str]) -> None:
9    for filename in filenames:
10        # This is where you would call your actual linter.
11        # For this example, we'll just create a dummy error for every file.
12        message = LintMessage(
13            path=filename,
14            line=1,
15            char=1,
16            code="EXAMPLE-101",
17            severity=LintSeverity.ERROR,
18            name="example-linter",
19            description="This is an example lint error.",
20        )
21        # Printing the message to stdout in JSON format is how lintrunner-adapters
22        # communicates with the lintrunner engine.
23        print(message.to_json())
24
25
26def main() -> None:
27    parser = argparse.ArgumentParser(description="Example lintrunner adapter.")
28    parser.add_argument(
29        "filenames",
30        nargs="+",
31        help="Paths to the files to lint.",
32    )
33    args = parser.parse_args()
34    run_lint(args.filenames)
35
36
37if __name__ == "__main__":
38    main()