Skip to content

Commit 5a6e76f

Browse files
[JsonReporter] Create a base JsonReporter
1 parent 61a461e commit 5a6e76f

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed

pylint/reporters/json_reporter.py

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,39 @@
77
from __future__ import annotations
88

99
import json
10-
from typing import TYPE_CHECKING
10+
from typing import TYPE_CHECKING, Any, Optional
1111

12-
from pylint.interfaces import UNDEFINED
12+
from pylint.interfaces import UNDEFINED, Confidence
1313
from pylint.message import Message
1414
from pylint.reporters.base_reporter import BaseReporter
1515
from pylint.typing import MessageLocationTuple
1616

1717
if TYPE_CHECKING:
18+
from typing import TypedDict
19+
1820
from pylint.lint.pylinter import PyLinter
1921
from pylint.reporters.ureports.nodes import Section
2022

23+
OldJsonExport = TypedDict(
24+
"OldJsonExport",
25+
{
26+
"type": str,
27+
"module": str,
28+
"obj": str,
29+
"line": int,
30+
"column": int,
31+
"endLine": Optional[int],
32+
"endColumn": Optional[int],
33+
"path": str,
34+
"symbol": str,
35+
"message": str,
36+
# "message-id" is not a valid name, so we have to define the TypedDict like this
37+
"message-id": str,
38+
},
39+
)
40+
2141

22-
class JSONReporter(BaseReporter):
42+
class BaseJSONReporter(BaseReporter):
2343
"""Report messages and layouts in JSON."""
2444

2545
name = "json"
@@ -30,9 +50,33 @@ def display_messages(self, layout: Section | None) -> None:
3050
json_dumpable = [self.serialize(message) for message in self.messages]
3151
print(json.dumps(json_dumpable, indent=4), file=self.out)
3252

53+
def display_reports(self, layout: Section) -> None:
54+
"""Don't do anything in this reporter."""
55+
56+
def _display(self, layout: Section) -> None:
57+
"""Do nothing."""
58+
59+
@staticmethod
60+
def serialize(message: Message) -> Any:
61+
raise NotImplementedError
62+
63+
@staticmethod
64+
def deserialize(message_as_json: Any) -> Message:
65+
raise NotImplementedError
66+
67+
68+
class JSONReporter(BaseJSONReporter):
69+
70+
"""
71+
TODO: 3.0: Remove this JSONReporter in favor of the new one handling abs-path
72+
and confidence.
73+
74+
TODO: 2.15: Add a new JSONReporter handling abs-path, confidence and scores.
75+
(Ultimately all other breaking change related to json for 3.0).
76+
"""
77+
3378
@staticmethod
34-
def serialize(message: Message) -> dict[str, int | str | None]:
35-
# TODO: 3.0: Add abs-path and confidence or a new JSONReporter
79+
def serialize(message: Message) -> OldJsonExport:
3680
return {
3781
"type": message.category,
3882
"module": message.module,
@@ -48,13 +92,13 @@ def serialize(message: Message) -> dict[str, int | str | None]:
4892
}
4993

5094
@staticmethod
51-
def deserialize(message_as_json: dict) -> Message:
95+
def deserialize(message_as_json: OldJsonExport) -> Message:
5296
return Message(
5397
msg_id=message_as_json["message-id"],
5498
symbol=message_as_json["symbol"],
5599
msg=message_as_json["message"],
56100
location=MessageLocationTuple(
57-
# TODO: 3.0: Add abs-path and confidence or a new JSONReporter
101+
# TODO: 3.0: Add abs-path and confidence in a new JSONReporter
58102
abspath=message_as_json["path"],
59103
path=message_as_json["path"],
60104
module=message_as_json["module"],
@@ -64,16 +108,10 @@ def deserialize(message_as_json: dict) -> Message:
64108
end_line=message_as_json["endLine"],
65109
end_column=message_as_json["endColumn"],
66110
),
67-
# TODO: 3.0: Make confidence available or a new JSONReporter
111+
# TODO: 3.0: Make confidence available in a new JSONReporter
68112
confidence=UNDEFINED,
69113
)
70114

71-
def display_reports(self, layout: Section) -> None:
72-
"""Don't do anything in this reporter."""
73-
74-
def _display(self, layout: Section) -> None:
75-
"""Do nothing."""
76-
77115

78116
def register(linter: PyLinter) -> None:
79117
linter.register_reporter(JSONReporter)

0 commit comments

Comments
 (0)