7
7
from __future__ import annotations
8
8
9
9
import json
10
- from abc import abstractmethod
11
- from typing import TYPE_CHECKING , Optional
10
+ from typing import TYPE_CHECKING , Any , Optional
12
11
13
- from pylint .interfaces import UNDEFINED
12
+ from pylint .interfaces import UNDEFINED , Confidence
14
13
from pylint .message import Message
15
14
from pylint .reporters .base_reporter import BaseReporter
16
15
from pylint .typing import MessageLocationTuple
39
38
},
40
39
)
41
40
41
+ class JSONExport (OldJsonExport ): # pylint: disable=inherit-non-class
42
+ confidence : Confidence
43
+ abspath : str
44
+
42
45
43
46
class BaseJSONReporter (BaseReporter ):
44
47
"""Report messages and layouts in JSON."""
@@ -57,26 +60,24 @@ def display_reports(self, layout: Section) -> None:
57
60
def _display (self , layout : Section ) -> None :
58
61
"""Do nothing."""
59
62
60
- @abstractmethod
61
- def serialize (self , message : Message ) -> OldJsonExport :
62
- ...
63
+ @staticmethod
64
+ def serialize (message : Message ) -> Any :
65
+ raise NotImplementedError
63
66
64
- @abstractmethod
65
- def deserialize (self , message_as_json : OldJsonExport ) -> Message :
66
- ...
67
+ @staticmethod
68
+ def deserialize (message_as_json : Any ) -> Message :
69
+ raise NotImplementedError
67
70
68
71
69
72
class JSONReporter (BaseJSONReporter ):
70
73
71
74
"""
72
75
TODO: 3.0: Remove this JSONReporter in favor of the new one handling abs-path
73
76
and confidence.
74
-
75
- TODO: 2.15: Add a new JSONReporter handling abs-path, confidence and scores.
76
- (Ultimately all other breaking change related to json for 3.0).
77
77
"""
78
78
79
- def serialize (self , message : Message ) -> OldJsonExport :
79
+ @staticmethod
80
+ def serialize (message : Message ) -> OldJsonExport :
80
81
return {
81
82
"type" : message .category ,
82
83
"module" : message .module ,
@@ -91,13 +92,13 @@ def serialize(self, message: Message) -> OldJsonExport:
91
92
"message-id" : message .msg_id ,
92
93
}
93
94
94
- def deserialize (self , message_as_json : OldJsonExport ) -> Message :
95
+ @staticmethod
96
+ def deserialize (message_as_json : OldJsonExport ) -> Message :
95
97
return Message (
96
98
msg_id = message_as_json ["message-id" ],
97
99
symbol = message_as_json ["symbol" ],
98
100
msg = message_as_json ["message" ],
99
101
location = MessageLocationTuple (
100
- # TODO: 3.0: Add abs-path and confidence in a new JSONReporter
101
102
abspath = message_as_json ["path" ],
102
103
path = message_as_json ["path" ],
103
104
module = message_as_json ["module" ],
@@ -107,10 +108,56 @@ def deserialize(self, message_as_json: OldJsonExport) -> Message:
107
108
end_line = message_as_json ["endLine" ],
108
109
end_column = message_as_json ["endColumn" ],
109
110
),
110
- # TODO: 3.0: Make confidence available in a new JSONReporter
111
111
confidence = UNDEFINED ,
112
112
)
113
113
114
114
115
+ class NewJSONReporter (BaseJSONReporter ):
116
+
117
+ """
118
+ TODO: 3.0: Refactor this reporter so it's the only reporter.
119
+
120
+ TODO: 3.0: Handle all other breaking changes related to json for 3.0
121
+ """
122
+
123
+ @staticmethod
124
+ def serialize (message : Message ) -> JSONExport :
125
+ return {
126
+ "type" : message .category ,
127
+ "module" : message .module ,
128
+ "obj" : message .obj ,
129
+ "line" : message .line ,
130
+ "column" : message .column ,
131
+ "endLine" : message .end_line ,
132
+ "endColumn" : message .end_column ,
133
+ "path" : message .path ,
134
+ "symbol" : message .symbol ,
135
+ "message" : message .msg or "" ,
136
+ "message-id" : message .msg_id ,
137
+ "confidence" : message .confidence ,
138
+ "abspath" : message .abspath ,
139
+ }
140
+
141
+ @staticmethod
142
+ def deserialize (message_as_json : JSONExport ) -> Message :
143
+ return Message (
144
+ msg_id = message_as_json ["message-id" ],
145
+ symbol = message_as_json ["symbol" ],
146
+ msg = message_as_json ["message" ],
147
+ location = MessageLocationTuple (
148
+ abspath = message_as_json ["abspath" ],
149
+ path = message_as_json ["path" ],
150
+ module = message_as_json ["module" ],
151
+ obj = message_as_json ["obj" ],
152
+ line = message_as_json ["line" ],
153
+ column = message_as_json ["column" ],
154
+ end_line = message_as_json ["endLine" ],
155
+ end_column = message_as_json ["endColumn" ],
156
+ ),
157
+ confidence = message_as_json ["confidence" ],
158
+ )
159
+
160
+
115
161
def register (linter : PyLinter ) -> None :
116
162
linter .register_reporter (JSONReporter )
163
+ linter .register_reporter (NewJSONReporter )
0 commit comments