Skip to content

Commit 80ef860

Browse files
Add a new JsonReporter with all information
1 parent 3570860 commit 80ef860

File tree

2 files changed

+89
-9
lines changed

2 files changed

+89
-9
lines changed

pylint/reporters/json_reporter.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111
from typing import TYPE_CHECKING, Optional
1212

13-
from pylint.interfaces import UNDEFINED
13+
from pylint.interfaces import UNDEFINED, Confidence
1414
from pylint.message import Message
1515
from pylint.reporters.base_reporter import BaseReporter
1616
from pylint.typing import MessageLocationTuple
@@ -43,6 +43,11 @@
4343
)
4444

4545

46+
class JSONExport(OldJsonExport): # pylint: disable=inherit-non-class
47+
confidence: Confidence
48+
abspath: str
49+
50+
4651
class BaseJSONReporter(BaseReporter):
4752
"""Report messages and layouts in JSON."""
4853

@@ -74,9 +79,6 @@ class JSONReporter(BaseJSONReporter):
7479
"""
7580
TODO: 3.0: Remove this JSONReporter in favor of the new one handling abs-path
7681
and confidence.
77-
78-
TODO: 3.0: Add a new JSONReporter handling abs-path, confidence and scores.
79-
(Ultimately all other breaking change related to json for 3.0).
8082
"""
8183

8284
@staticmethod
@@ -102,7 +104,6 @@ def deserialize(message_as_json: OldJsonExport) -> Message:
102104
symbol=message_as_json["symbol"],
103105
msg=message_as_json["message"],
104106
location=MessageLocationTuple(
105-
# TODO: 3.0: Add abs-path and confidence in a new JSONReporter
106107
abspath=message_as_json["path"],
107108
path=message_as_json["path"],
108109
module=message_as_json["module"],
@@ -112,10 +113,56 @@ def deserialize(message_as_json: OldJsonExport) -> Message:
112113
end_line=message_as_json["endLine"],
113114
end_column=message_as_json["endColumn"],
114115
),
115-
# TODO: 3.0: Make confidence available in a new JSONReporter
116116
confidence=UNDEFINED,
117117
)
118118

119119

120+
class NewJSONReporter(BaseJSONReporter):
121+
122+
"""
123+
TODO: 3.0: Refactor this reporter so it's the only reporter.
124+
125+
TODO: 3.0: Handle all other breaking changes related to json for 3.0
126+
"""
127+
128+
@staticmethod
129+
def serialize(message: Message) -> JSONExport:
130+
return {
131+
"type": message.category,
132+
"module": message.module,
133+
"obj": message.obj,
134+
"line": message.line,
135+
"column": message.column,
136+
"endLine": message.end_line,
137+
"endColumn": message.end_column,
138+
"path": message.path,
139+
"symbol": message.symbol,
140+
"message": message.msg or "",
141+
"message-id": message.msg_id,
142+
"confidence": message.confidence,
143+
"abspath": message.abspath,
144+
}
145+
146+
@staticmethod
147+
def deserialize(message_as_json: JSONExport) -> Message:
148+
return Message(
149+
msg_id=message_as_json["message-id"],
150+
symbol=message_as_json["symbol"],
151+
msg=message_as_json["message"],
152+
location=MessageLocationTuple(
153+
abspath=message_as_json["abspath"],
154+
path=message_as_json["path"],
155+
module=message_as_json["module"],
156+
obj=message_as_json["obj"],
157+
line=message_as_json["line"],
158+
column=message_as_json["column"],
159+
end_line=message_as_json["endLine"],
160+
end_column=message_as_json["endColumn"],
161+
),
162+
confidence=message_as_json["confidence"],
163+
)
164+
165+
120166
def register(linter: PyLinter) -> None:
121167
linter.register_reporter(JSONReporter)
168+
linter.register_reporter(NewJSONReporter)

tests/reporters/unittest_json_reporter.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88

99
import json
1010
from io import StringIO
11+
from pathlib import Path
1112
from typing import Any
1213

1314
import pytest
1415

1516
from pylint import checkers
16-
from pylint.interfaces import UNDEFINED
17+
from pylint.interfaces import HIGH, UNDEFINED
1718
from pylint.lint import PyLinter
1819
from pylint.message import Message
1920
from pylint.reporters import JSONReporter
21+
from pylint.reporters.json_reporter import NewJSONReporter
2022
from pylint.reporters.ureports.nodes import EvaluationSection
2123
from pylint.typing import MessageLocationTuple
2224

@@ -133,5 +135,36 @@ def get_linter_result(score: bool, message: dict[str, Any]) -> list[dict[str, An
133135
)
134136
def test_serialize_deserialize(message: Message) -> None:
135137
# TODO: 3.0: Add confidence handling, add path and abs path handling or a new JSONReporter
136-
json_message = JSONReporter.serialize(message)
137-
assert message == JSONReporter.deserialize(json_message)
138+
for reporter in (JSONReporter, NewJSONReporter):
139+
json_message = reporter.serialize(message)
140+
assert message == reporter.deserialize(json_message)
141+
142+
143+
@pytest.mark.parametrize(
144+
"message",
145+
[
146+
pytest.param(
147+
Message(
148+
msg_id="C0111",
149+
symbol="missing-docstring",
150+
location=MessageLocationTuple(
151+
abspath=str(Path(__file__).resolve()),
152+
path=__file__,
153+
module="unittest_json_reporter",
154+
obj="obj",
155+
line=1,
156+
column=3,
157+
end_line=3,
158+
end_column=5,
159+
),
160+
msg="This is the actual message",
161+
confidence=HIGH,
162+
),
163+
id="everything-defined",
164+
)
165+
],
166+
)
167+
def test_serialize_deserialize_new_json(message: Message) -> None:
168+
json_message = NewJSONReporter.serialize(message)
169+
print(json_message)
170+
assert message == NewJSONReporter.deserialize(json_message)

0 commit comments

Comments
 (0)