77from __future__ import annotations
88
99import json
10- from typing import TYPE_CHECKING
10+ import sys
11+ from typing import TYPE_CHECKING , Optional
1112
1213from pylint .interfaces import UNDEFINED
1314from pylint .message import Message
1415from pylint .reporters .base_reporter import BaseReporter
1516from pylint .typing import MessageLocationTuple
1617
18+ if sys .version_info >= (3 , 8 ):
19+ from typing import TypedDict
20+ else :
21+ from typing_extensions import TypedDict
22+
1723if TYPE_CHECKING :
1824 from pylint .lint .pylinter import PyLinter
1925 from pylint .reporters .ureports .nodes import Section
2026
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+
2145
22- class JSONReporter (BaseReporter ):
46+ class BaseJSONReporter (BaseReporter ):
2347 """Report messages and layouts in JSON."""
2448
2549 name = "json"
@@ -30,9 +54,33 @@ def display_messages(self, layout: Section | None) -> None:
3054 json_dumpable = [self .serialize (message ) for message in self .messages ]
3155 print (json .dumps (json_dumpable , indent = 4 ), file = self .out )
3256
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+
3382 @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 :
3684 return {
3785 "type" : message .category ,
3886 "module" : message .module ,
@@ -48,13 +96,13 @@ def serialize(message: Message) -> dict[str, int | str | None]:
4896 }
4997
5098 @staticmethod
51- def deserialize (message_as_json : dict ) -> Message :
99+ def deserialize (message_as_json : OldJsonExport ) -> Message :
52100 return Message (
53101 msg_id = message_as_json ["message-id" ],
54102 symbol = message_as_json ["symbol" ],
55103 msg = message_as_json ["message" ],
56104 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
58106 abspath = message_as_json ["path" ],
59107 path = message_as_json ["path" ],
60108 module = message_as_json ["module" ],
@@ -64,16 +112,10 @@ def deserialize(message_as_json: dict) -> Message:
64112 end_line = message_as_json ["endLine" ],
65113 end_column = message_as_json ["endColumn" ],
66114 ),
67- # TODO: 3.0: Make confidence available or a new JSONReporter
115+ # TODO: 3.0: Make confidence available in a new JSONReporter
68116 confidence = UNDEFINED ,
69117 )
70118
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-
77119
78120def register (linter : PyLinter ) -> None :
79121 linter .register_reporter (JSONReporter )
0 commit comments