7
7
from __future__ import annotations
8
8
9
9
import json
10
- from typing import TYPE_CHECKING
10
+ import sys
11
+ from typing import TYPE_CHECKING , Optional
11
12
12
13
from pylint .interfaces import UNDEFINED
13
14
from pylint .message import Message
14
15
from pylint .reporters .base_reporter import BaseReporter
15
16
from pylint .typing import MessageLocationTuple
16
17
18
+ if sys .version_info >= (3 , 8 ):
19
+ from typing import TypedDict
20
+ else :
21
+ from typing_extensions import TypedDict
22
+
17
23
if TYPE_CHECKING :
18
24
from pylint .lint .pylinter import PyLinter
19
25
from pylint .reporters .ureports .nodes import Section
20
26
27
+ # Since message-id is an invalid name we need to use the alternative syntax
28
+ OldJsonExport = TypedDict (
29
+ "OldJsonExport" ,
30
+ {
31
+ "type" : str ,
32
+ "module" : str ,
33
+ "obj" : str ,
34
+ "line" : int ,
35
+ "column" : int ,
36
+ "endLine" : Optional [int ],
37
+ "endColumn" : Optional [int ],
38
+ "path" : str ,
39
+ "symbol" : str ,
40
+ "message" : str ,
41
+ "message-id" : str ,
42
+ },
43
+ )
44
+
21
45
22
- class JSONReporter (BaseReporter ):
46
+ class BaseJSONReporter (BaseReporter ):
23
47
"""Report messages and layouts in JSON."""
24
48
25
49
name = "json"
@@ -30,9 +54,33 @@ def display_messages(self, layout: Section | None) -> None:
30
54
json_dumpable = [self .serialize (message ) for message in self .messages ]
31
55
print (json .dumps (json_dumpable , indent = 4 ), file = self .out )
32
56
57
+ def display_reports (self , layout : Section ) -> None :
58
+ """Don't do anything in this reporter."""
59
+
60
+ def _display (self , layout : Section ) -> None :
61
+ """Do nothing."""
62
+
63
+ @staticmethod
64
+ def serialize (message : Message ) -> OldJsonExport :
65
+ raise NotImplementedError
66
+
67
+ @staticmethod
68
+ def deserialize (message_as_json : OldJsonExport ) -> Message :
69
+ raise NotImplementedError
70
+
71
+
72
+ class JSONReporter (BaseJSONReporter ):
73
+
74
+ """
75
+ TODO: 3.0: Remove this JSONReporter in favor of the new one handling abs-path
76
+ and confidence.
77
+
78
+ TODO: 2.15: Add a new JSONReporter handling abs-path, confidence and scores.
79
+ (Ultimately all other breaking change related to json for 3.0).
80
+ """
81
+
33
82
@staticmethod
34
- def serialize (message : Message ) -> dict [str , int | str | None ]:
35
- # TODO: 3.0: Add abs-path and confidence or a new JSONReporter
83
+ def serialize (message : Message ) -> OldJsonExport :
36
84
return {
37
85
"type" : message .category ,
38
86
"module" : message .module ,
@@ -48,13 +96,13 @@ def serialize(message: Message) -> dict[str, int | str | None]:
48
96
}
49
97
50
98
@staticmethod
51
- def deserialize (message_as_json : dict ) -> Message :
99
+ def deserialize (message_as_json : OldJsonExport ) -> Message :
52
100
return Message (
53
101
msg_id = message_as_json ["message-id" ],
54
102
symbol = message_as_json ["symbol" ],
55
103
msg = message_as_json ["message" ],
56
104
location = MessageLocationTuple (
57
- # TODO: 3.0: Add abs-path and confidence or a new JSONReporter
105
+ # TODO: 3.0: Add abs-path and confidence in a new JSONReporter
58
106
abspath = message_as_json ["path" ],
59
107
path = message_as_json ["path" ],
60
108
module = message_as_json ["module" ],
@@ -64,16 +112,10 @@ def deserialize(message_as_json: dict) -> Message:
64
112
end_line = message_as_json ["endLine" ],
65
113
end_column = message_as_json ["endColumn" ],
66
114
),
67
- # TODO: 3.0: Make confidence available or a new JSONReporter
115
+ # TODO: 3.0: Make confidence available in a new JSONReporter
68
116
confidence = UNDEFINED ,
69
117
)
70
118
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
119
78
120
def register (linter : PyLinter ) -> None :
79
121
linter .register_reporter (JSONReporter )
0 commit comments