7
7
from __future__ import annotations
8
8
9
9
import json
10
- from typing import TYPE_CHECKING
10
+ from typing import TYPE_CHECKING , Any , Optional
11
11
12
- from pylint .interfaces import UNDEFINED
12
+ from pylint .interfaces import UNDEFINED , Confidence
13
13
from pylint .message import Message
14
14
from pylint .reporters .base_reporter import BaseReporter
15
15
from pylint .typing import MessageLocationTuple
16
16
17
17
if TYPE_CHECKING :
18
+ from typing import TypedDict
19
+
18
20
from pylint .lint .pylinter import PyLinter
19
21
from pylint .reporters .ureports .nodes import Section
20
22
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
+
21
41
22
- class JSONReporter (BaseReporter ):
42
+ class BaseJSONReporter (BaseReporter ):
23
43
"""Report messages and layouts in JSON."""
24
44
25
45
name = "json"
@@ -30,9 +50,33 @@ def display_messages(self, layout: Section | None) -> None:
30
50
json_dumpable = [self .serialize (message ) for message in self .messages ]
31
51
print (json .dumps (json_dumpable , indent = 4 ), file = self .out )
32
52
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
+
33
78
@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 :
36
80
return {
37
81
"type" : message .category ,
38
82
"module" : message .module ,
@@ -48,13 +92,13 @@ def serialize(message: Message) -> dict[str, int | str | None]:
48
92
}
49
93
50
94
@staticmethod
51
- def deserialize (message_as_json : dict ) -> Message :
95
+ def deserialize (message_as_json : OldJsonExport ) -> Message :
52
96
return Message (
53
97
msg_id = message_as_json ["message-id" ],
54
98
symbol = message_as_json ["symbol" ],
55
99
msg = message_as_json ["message" ],
56
100
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
58
102
abspath = message_as_json ["path" ],
59
103
path = message_as_json ["path" ],
60
104
module = message_as_json ["module" ],
@@ -64,16 +108,10 @@ def deserialize(message_as_json: dict) -> Message:
64
108
end_line = message_as_json ["endLine" ],
65
109
end_column = message_as_json ["endColumn" ],
66
110
),
67
- # TODO: 3.0: Make confidence available or a new JSONReporter
111
+ # TODO: 3.0: Make confidence available in a new JSONReporter
68
112
confidence = UNDEFINED ,
69
113
)
70
114
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
-
77
115
78
116
def register (linter : PyLinter ) -> None :
79
117
linter .register_reporter (JSONReporter )
0 commit comments