Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions pylint/reporters/json_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import sys
from typing import TYPE_CHECKING, Optional

from pylint.interfaces import UNDEFINED
from pylint.interfaces import UNDEFINED, Confidence
from pylint.message import Message
from pylint.reporters.base_reporter import BaseReporter
from pylint.typing import MessageLocationTuple
Expand Down Expand Up @@ -43,6 +43,11 @@
)


class JSONExport(OldJsonExport):
confidence: Confidence
abspath: str


class BaseJSONReporter(BaseReporter):
"""Report messages and layouts in JSON."""

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


class JSONReporter(BaseJSONReporter):
class OldJSONReporter(BaseJSONReporter):

"""
TODO: 3.0: Remove this JSONReporter in favor of the new one handling abs-path
and confidence.

TODO: 3.0: Add a new JSONReporter handling abs-path, confidence and scores.
(Ultimately all other breaking change related to json for 3.0).
"""
"""TODO: Remove in 4.0."""

@staticmethod
def serialize(message: Message) -> OldJsonExport:
Expand All @@ -102,7 +101,6 @@ def deserialize(message_as_json: OldJsonExport) -> Message:
symbol=message_as_json["symbol"],
msg=message_as_json["message"],
location=MessageLocationTuple(
# TODO: 3.0: Add abs-path and confidence in a new JSONReporter
abspath=message_as_json["path"],
path=message_as_json["path"],
module=message_as_json["module"],
Expand All @@ -112,10 +110,49 @@ def deserialize(message_as_json: OldJsonExport) -> Message:
end_line=message_as_json["endLine"],
end_column=message_as_json["endColumn"],
),
# TODO: 3.0: Make confidence available in a new JSONReporter
confidence=UNDEFINED,
)


class JSONReporter(BaseJSONReporter):
@staticmethod
def serialize(message: Message) -> JSONExport:
return {
"type": message.category,
"module": message.module,
"obj": message.obj,
"line": message.line,
"column": message.column,
"endLine": message.end_line,
"endColumn": message.end_column,
"path": message.path,
"symbol": message.symbol,
"message": message.msg or "",
"message-id": message.msg_id,
"confidence": message.confidence,
"abspath": message.abspath,
}

@staticmethod
def deserialize(message_as_json: JSONExport) -> Message:
return Message(
msg_id=message_as_json["message-id"],
symbol=message_as_json["symbol"],
msg=message_as_json["message"],
location=MessageLocationTuple(
abspath=message_as_json["abspath"],
path=message_as_json["path"],
module=message_as_json["module"],
obj=message_as_json["obj"],
line=message_as_json["line"],
column=message_as_json["column"],
end_line=message_as_json["endLine"],
end_column=message_as_json["endColumn"],
),
confidence=message_as_json["confidence"],
)


def register(linter: PyLinter) -> None:
linter.register_reporter(OldJSONReporter)
linter.register_reporter(JSONReporter)
39 changes: 36 additions & 3 deletions tests/reporters/unittest_json_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@

import json
from io import StringIO
from pathlib import Path
from typing import Any

import pytest

from pylint import checkers
from pylint.interfaces import UNDEFINED
from pylint.interfaces import HIGH, UNDEFINED
from pylint.lint import PyLinter
from pylint.message import Message
from pylint.reporters import JSONReporter
from pylint.reporters.json_reporter import NewJSONReporter
from pylint.reporters.ureports.nodes import EvaluationSection
from pylint.typing import MessageLocationTuple

Expand Down Expand Up @@ -133,5 +135,36 @@ def get_linter_result(score: bool, message: dict[str, Any]) -> list[dict[str, An
)
def test_serialize_deserialize(message: Message) -> None:
# TODO: 3.0: Add confidence handling, add path and abs path handling or a new JSONReporter
json_message = JSONReporter.serialize(message)
assert message == JSONReporter.deserialize(json_message)
for reporter in (JSONReporter, NewJSONReporter):
json_message = reporter.serialize(message)
assert message == reporter.deserialize(json_message)


@pytest.mark.parametrize(
"message",
[
pytest.param(
Message(
msg_id="C0111",
symbol="missing-docstring",
location=MessageLocationTuple(
abspath=str(Path(__file__).resolve()),
path=__file__,
module="unittest_json_reporter",
obj="obj",
line=1,
column=3,
end_line=3,
end_column=5,
),
msg="This is the actual message",
confidence=HIGH,
),
id="everything-defined",
)
],
)
def test_serialize_deserialize_new_json(message: Message) -> None:
json_message = NewJSONReporter.serialize(message)
print(json_message)
assert message == NewJSONReporter.deserialize(json_message)