9
9
import json
10
10
from typing import TYPE_CHECKING
11
11
12
+ from pylint .interfaces import UNDEFINED
13
+ from pylint .message import Message
12
14
from pylint .reporters .base_reporter import BaseReporter
15
+ from pylint .typing import MessageLocationTuple
13
16
14
17
if TYPE_CHECKING :
15
18
from pylint .lint .pylinter import PyLinter
@@ -24,24 +27,47 @@ class JSONReporter(BaseReporter):
24
27
25
28
def display_messages (self , layout : Section | None ) -> None :
26
29
"""Launch layouts display."""
27
- json_dumpable = [
28
- {
29
- "type" : msg .category ,
30
- "module" : msg .module ,
31
- "obj" : msg .obj ,
32
- "line" : msg .line ,
33
- "column" : msg .column ,
34
- "endLine" : msg .end_line ,
35
- "endColumn" : msg .end_column ,
36
- "path" : msg .path ,
37
- "symbol" : msg .symbol ,
38
- "message" : msg .msg or "" ,
39
- "message-id" : msg .msg_id ,
40
- }
41
- for msg in self .messages
42
- ]
30
+ json_dumpable = [self .serialize (message ) for message in self .messages ]
43
31
print (json .dumps (json_dumpable , indent = 4 ), file = self .out )
44
32
33
+ @staticmethod
34
+ def serialize (message : Message ) -> dict [str , int | str | None ]:
35
+ # TODO (3.0) add abspath and confidence
36
+ return {
37
+ "type" : message .category ,
38
+ "module" : message .module ,
39
+ "obj" : message .obj ,
40
+ "line" : message .line ,
41
+ "column" : message .column ,
42
+ "endLine" : message .end_line ,
43
+ "endColumn" : message .end_column ,
44
+ "path" : message .path ,
45
+ "symbol" : message .symbol ,
46
+ "message" : message .msg or "" ,
47
+ "message-id" : message .msg_id ,
48
+ }
49
+
50
+ @staticmethod
51
+ def deserialize (message_as_json : dict ) -> Message :
52
+ return Message (
53
+ msg_id = message_as_json ["message-id" ],
54
+ symbol = message_as_json ["symbol" ],
55
+ msg = message_as_json ["message" ],
56
+ location = MessageLocationTuple (
57
+ # TODO (3.0) abspath is not available in json export
58
+ abspath = message_as_json ["path" ],
59
+ path = message_as_json ["path" ],
60
+ module = message_as_json ["module" ],
61
+ obj = message_as_json ["obj" ],
62
+ line = message_as_json ["line" ],
63
+ column = message_as_json ["column" ],
64
+ end_line = message_as_json ["endLine" ],
65
+ end_column = message_as_json ["endColumn" ],
66
+ ),
67
+ # TODO (3.0) confidence is not available in json export
68
+ confidence = UNDEFINED ,
69
+ )
70
+
45
71
def display_reports (self , layout : Section ) -> None :
46
72
"""Don't do anything in this reporter."""
47
73
0 commit comments