5
5
from argparse import ArgumentParser
6
6
from collections import defaultdict
7
7
from distutils import dir_util , spawn
8
- from typing import Dict , List , Pattern , Union
8
+ from typing import DefaultDict , List , Pattern , Union
9
9
10
10
from scripts .git_helpers import git_checkout_drf
11
11
from scripts .paths import DRF_SOURCE_DIRECTORY , PROJECT_DIRECTORY , STUBS_DIRECTORY
14
14
# This should be updated periodically or after every DRF release.
15
15
DRF_GIT_REF = "22d206c1e0dbc03840c4d190f7eda537c0f2010a"
16
16
17
- IGNORED_MODULES = []
18
- MOCK_OBJECTS = [
17
+ IGNORED_MODULES : List [ str ] = []
18
+ MOCK_OBJECTS : List [ str ] = [
19
19
"MockQueryset" ,
20
20
"MockRequest" ,
21
21
"TypeErrorQueryset" ,
253
253
"utils.py" : ['Invalid signature "Callable[[BadType], Any]"' ],
254
254
}
255
255
256
+ _DictToSearch = DefaultDict [str , DefaultDict [Union [str , Pattern [str ]], int ]]
256
257
257
- def get_unused_ignores (ignored_message_freq : Dict [str , Dict [Union [str , Pattern ], int ]]) -> List [str ]:
258
+
259
+ def get_unused_ignores (ignored_message_freq : _DictToSearch ) -> List [str ]:
258
260
unused_ignores = []
259
261
for root_key , patterns in IGNORED_ERRORS .items ():
260
262
for pattern in patterns :
@@ -265,7 +267,7 @@ def get_unused_ignores(ignored_message_freq: Dict[str, Dict[Union[str, Pattern],
265
267
return unused_ignores
266
268
267
269
268
- def is_pattern_fits (pattern : Union [Pattern , str ], line : str ):
270
+ def does_pattern_fit (pattern : Union [Pattern [ str ] , str ], line : str ) -> bool :
269
271
if isinstance (pattern , Pattern ):
270
272
if pattern .search (line ):
271
273
return True
@@ -275,15 +277,15 @@ def is_pattern_fits(pattern: Union[Pattern, str], line: str):
275
277
return False
276
278
277
279
278
- def is_ignored (line : str , filename : str , ignored_message_dict : Dict [ str , Dict [ str , int ]] ) -> bool :
280
+ def is_ignored (line : str , filename : str , * , ignored_message_freqs : _DictToSearch ) -> bool :
279
281
if "runtests" in line or filename in IGNORED_MODULES :
280
282
return True
281
283
for pattern in IGNORED_ERRORS ["__common__" ]:
282
284
if pattern in line :
283
285
return True
284
286
for pattern in IGNORED_ERRORS .get (filename , []):
285
- if is_pattern_fits (pattern , line ):
286
- ignored_message_dict [test_filename ][pattern ] += 1
287
+ if does_pattern_fit (pattern , line ):
288
+ ignored_message_freqs [test_filename ][pattern ] += 1
287
289
return True
288
290
for mock_object in MOCK_OBJECTS :
289
291
if mock_object in line :
@@ -322,13 +324,13 @@ def update(self, op_code, cur_count, max_count=None, message=""):
322
324
mypy_executable = spawn .find_executable ("mypy" )
323
325
mypy_argv = [mypy_executable , * mypy_options ]
324
326
completed = subprocess .run (
325
- mypy_argv ,
327
+ mypy_argv , # type: ignore
326
328
env = {"PYTHONPATH" : str (PROJECT_DIRECTORY )},
327
329
stdout = subprocess .PIPE ,
328
330
stderr = subprocess .STDOUT ,
329
331
)
330
332
output = completed .stdout .decode ()
331
- ignored_message_freqs = defaultdict (lambda : defaultdict (int ))
333
+ ignored_message_freqs : _DictToSearch = defaultdict (lambda : defaultdict (int ))
332
334
sorted_lines = sorted (output .splitlines ())
333
335
for line in sorted_lines :
334
336
try :
@@ -337,7 +339,7 @@ def update(self, op_code, cur_count, max_count=None, message=""):
337
339
except IndexError :
338
340
test_filename = "unknown"
339
341
340
- if not is_ignored (line , test_filename , ignored_message_dict = ignored_message_freqs ):
342
+ if not is_ignored (line , test_filename , ignored_message_freqs = ignored_message_freqs ):
341
343
global_rc = 1
342
344
print (line )
343
345
0 commit comments