1
1
import json
2
2
import os .path
3
+ import re
3
4
import sys
4
5
import traceback
5
6
from pathlib import Path
8
9
from collections import defaultdict
9
10
10
11
from typing import Tuple , List , TypeVar , Set , Dict , Optional , TextIO , Callable
11
- from typing_extensions import Final
12
+ from typing_extensions import Final , TypedDict
12
13
13
14
from mypy .scope import Scope
14
15
from mypy .options import Options
@@ -140,18 +141,10 @@ def filter_prefix(error_map: Dict[str, List[ErrorInfo]]) -> Dict[str, List[Error
140
141
return result
141
142
142
143
143
- def baseline_json_hook (d : Dict [str , object ]) -> object :
144
- class_ = d .pop (".class" , None )
145
- if class_ is None :
146
- return d
147
- if class_ == "mypy.errors.ErrorInfo" :
148
- result = object .__new__ (ErrorInfo )
149
- elif class_ == "mypy.errorcodes.ErrorCode" :
150
- result = object .__new__ (ErrorCode )
151
- else :
152
- raise Exception (f"unknown class { class_ !r} " )
153
- result .__dict__ = d
154
- return result
144
+ class BaselineError (TypedDict ):
145
+ line : int
146
+ code : str
147
+ message : str
155
148
156
149
157
150
class Errors :
@@ -211,7 +204,7 @@ class Errors:
211
204
seen_import_error = False
212
205
213
206
# Error baseline
214
- baseline : Dict [str , List [ErrorInfo ]] = {}
207
+ baseline : Dict [str , List [BaselineError ]] = {}
215
208
# All detected errors before baseline filter
216
209
all_errors : Dict [str , List [ErrorInfo ]] = {}
217
210
@@ -808,20 +801,26 @@ def save_baseline(self, file: Path) -> None:
808
801
if not file .parent .exists ():
809
802
file .parent .mkdir ()
810
803
json .dump (
811
- filter_prefix (self .error_info_map ),
804
+ {
805
+ file : [
806
+ {
807
+ "line" : error .line ,
808
+ "code" : error .code .code ,
809
+ "message" : error .message
810
+ } for error in errors
811
+ ]
812
+ for file , errors in filter_prefix (self .error_info_map ).items ()
813
+ },
812
814
file .open ("w" ),
813
- default = lambda o : {
814
- ** {".class" : type (o ).__module__ + "." + type (o ).__qualname__ },
815
- ** o .__dict__ ,
816
- }
815
+ indent = 2 ,
817
816
)
818
817
819
818
def load_baseline (self , file : Path ) -> bool :
820
819
"""Load baseline errors from baseline file"""
821
820
822
821
if not file .exists ():
823
822
return False
824
- self .baseline = json .load (file .open ("r" ), object_hook = baseline_json_hook )
823
+ self .baseline = json .load (file .open ("r" ))
825
824
return True
826
825
827
826
def filter_baseline (self ) -> None :
@@ -837,16 +836,22 @@ def filter_baseline(self) -> None:
837
836
for error in errors :
838
837
for baseline_error in baseline_errors :
839
838
if (
840
- error .line == baseline_error .line and error .code == baseline_error .code
841
- or error .message == baseline_error .message and
842
- abs (error .line - baseline_error .line ) < 50
839
+ error .line == baseline_error ["line" ] and
840
+ error .code .code == baseline_error ["code" ]
841
+ or clean_baseline_message (error .message ) ==
842
+ clean_baseline_message (baseline_error ["message" ]) and
843
+ abs (error .line - baseline_error ["line" ]) < 50
843
844
):
844
845
break
845
846
else :
846
847
new_errors .append (error )
847
848
self .error_info_map [file ] = new_errors
848
849
849
850
851
+ def clean_baseline_message (message : str ) -> str :
852
+ return re .sub (r"line \r." , message , "" )
853
+
854
+
850
855
class CompileError (Exception ):
851
856
"""Exception raised when there is a compile error.
852
857
0 commit comments