Skip to content

Commit 885a129

Browse files
wip
1 parent e9894d3 commit 885a129

File tree

2 files changed

+84
-14
lines changed

2 files changed

+84
-14
lines changed

pylint/reporters/json_reporter.py

Lines changed: 48 additions & 11 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):
47+
confidence: Confidence
48+
abspath: str
49+
50+
4651
class BaseJSONReporter(BaseReporter):
4752
"""Report messages and layouts in JSON."""
4853

@@ -69,15 +74,9 @@ def deserialize(message_as_json: OldJsonExport) -> Message:
6974
raise NotImplementedError
7075

7176

72-
class JSONReporter(BaseJSONReporter):
77+
class OldJSONReporter(BaseJSONReporter):
7378

74-
"""
75-
TODO: 3.0: Remove this JSONReporter in favor of the new one handling abs-path
76-
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).
80-
"""
79+
"""TODO: Remove in 4.0."""
8180

8281
@staticmethod
8382
def serialize(message: Message) -> OldJsonExport:
@@ -102,7 +101,6 @@ def deserialize(message_as_json: OldJsonExport) -> Message:
102101
symbol=message_as_json["symbol"],
103102
msg=message_as_json["message"],
104103
location=MessageLocationTuple(
105-
# TODO: 3.0: Add abs-path and confidence in a new JSONReporter
106104
abspath=message_as_json["path"],
107105
path=message_as_json["path"],
108106
module=message_as_json["module"],
@@ -112,10 +110,49 @@ def deserialize(message_as_json: OldJsonExport) -> Message:
112110
end_line=message_as_json["endLine"],
113111
end_column=message_as_json["endColumn"],
114112
),
115-
# TODO: 3.0: Make confidence available in a new JSONReporter
116113
confidence=UNDEFINED,
117114
)
118115

119116

117+
class JSONReporter(BaseJSONReporter):
118+
@staticmethod
119+
def serialize(message: Message) -> JSONExport:
120+
return {
121+
"type": message.category,
122+
"module": message.module,
123+
"obj": message.obj,
124+
"line": message.line,
125+
"column": message.column,
126+
"endLine": message.end_line,
127+
"endColumn": message.end_column,
128+
"path": message.path,
129+
"symbol": message.symbol,
130+
"message": message.msg or "",
131+
"message-id": message.msg_id,
132+
"confidence": message.confidence,
133+
"abspath": message.abspath,
134+
}
135+
136+
@staticmethod
137+
def deserialize(message_as_json: JSONExport) -> Message:
138+
return Message(
139+
msg_id=message_as_json["message-id"],
140+
symbol=message_as_json["symbol"],
141+
msg=message_as_json["message"],
142+
location=MessageLocationTuple(
143+
abspath=message_as_json["abspath"],
144+
path=message_as_json["path"],
145+
module=message_as_json["module"],
146+
obj=message_as_json["obj"],
147+
line=message_as_json["line"],
148+
column=message_as_json["column"],
149+
end_line=message_as_json["endLine"],
150+
end_column=message_as_json["endColumn"],
151+
),
152+
confidence=message_as_json["confidence"],
153+
)
154+
155+
120156
def register(linter: PyLinter) -> None:
157+
linter.register_reporter(OldJSONReporter)
121158
linter.register_reporter(JSONReporter)

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)