summaryrefslogtreecommitdiff
path: root/pylint/reporters
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2022-06-28 09:30:01 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2022-06-29 10:19:55 +0200
commitdee2f1449139aa013906cec685c05a8c1d5e6360 (patch)
treed3ff9b8049262dafd2e563fe7ec5aacc5df76626 /pylint/reporters
parent640f92449d0e76c1bbad4084867030c470dc4689 (diff)
downloadpylint-git-dee2f1449139aa013906cec685c05a8c1d5e6360.tar.gz
[JsonReporter] Create a base JsonReporter
Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
Diffstat (limited to 'pylint/reporters')
-rw-r--r--pylint/reporters/json_reporter.py68
1 files changed, 55 insertions, 13 deletions
diff --git a/pylint/reporters/json_reporter.py b/pylint/reporters/json_reporter.py
index 1d6d6ab86..c47aac598 100644
--- a/pylint/reporters/json_reporter.py
+++ b/pylint/reporters/json_reporter.py
@@ -7,19 +7,43 @@
from __future__ import annotations
import json
-from typing import TYPE_CHECKING
+import sys
+from typing import TYPE_CHECKING, Optional
from pylint.interfaces import UNDEFINED
from pylint.message import Message
from pylint.reporters.base_reporter import BaseReporter
from pylint.typing import MessageLocationTuple
+if sys.version_info >= (3, 8):
+ from typing import TypedDict
+else:
+ from typing_extensions import TypedDict
+
if TYPE_CHECKING:
from pylint.lint.pylinter import PyLinter
from pylint.reporters.ureports.nodes import Section
+# Since message-id is an invalid name we need to use the alternative syntax
+OldJsonExport = TypedDict(
+ "OldJsonExport",
+ {
+ "type": str,
+ "module": str,
+ "obj": str,
+ "line": int,
+ "column": int,
+ "endLine": Optional[int],
+ "endColumn": Optional[int],
+ "path": str,
+ "symbol": str,
+ "message": str,
+ "message-id": str,
+ },
+)
+
-class JSONReporter(BaseReporter):
+class BaseJSONReporter(BaseReporter):
"""Report messages and layouts in JSON."""
name = "json"
@@ -30,9 +54,33 @@ class JSONReporter(BaseReporter):
json_dumpable = [self.serialize(message) for message in self.messages]
print(json.dumps(json_dumpable, indent=4), file=self.out)
+ def display_reports(self, layout: Section) -> None:
+ """Don't do anything in this reporter."""
+
+ def _display(self, layout: Section) -> None:
+ """Do nothing."""
+
+ @staticmethod
+ def serialize(message: Message) -> OldJsonExport:
+ raise NotImplementedError
+
+ @staticmethod
+ def deserialize(message_as_json: OldJsonExport) -> Message:
+ raise NotImplementedError
+
+
+class JSONReporter(BaseJSONReporter):
+
+ """
+ TODO: 3.0: Remove this JSONReporter in favor of the new one handling abs-path
+ and confidence.
+
+ TODO: 2.15: Add a new JSONReporter handling abs-path, confidence and scores.
+ (Ultimately all other breaking change related to json for 3.0).
+ """
+
@staticmethod
- def serialize(message: Message) -> dict[str, int | str | None]:
- # TODO: 3.0: Add abs-path and confidence or a new JSONReporter
+ def serialize(message: Message) -> OldJsonExport:
return {
"type": message.category,
"module": message.module,
@@ -48,13 +96,13 @@ class JSONReporter(BaseReporter):
}
@staticmethod
- def deserialize(message_as_json: dict) -> Message:
+ def deserialize(message_as_json: OldJsonExport) -> Message:
return Message(
msg_id=message_as_json["message-id"],
symbol=message_as_json["symbol"],
msg=message_as_json["message"],
location=MessageLocationTuple(
- # TODO: 3.0: Add abs-path and confidence or a new JSONReporter
+ # 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"],
@@ -64,16 +112,10 @@ class JSONReporter(BaseReporter):
end_line=message_as_json["endLine"],
end_column=message_as_json["endColumn"],
),
- # TODO: 3.0: Make confidence available or a new JSONReporter
+ # TODO: 3.0: Make confidence available in a new JSONReporter
confidence=UNDEFINED,
)
- def display_reports(self, layout: Section) -> None:
- """Don't do anything in this reporter."""
-
- def _display(self, layout: Section) -> None:
- """Do nothing."""
-
def register(linter: PyLinter) -> None:
linter.register_reporter(JSONReporter)