16
16
import sys
17
17
import tempfile
18
18
from pathlib import Path
19
- from typing import Dict , NamedTuple
19
+ from typing import TYPE_CHECKING , Dict , List , NamedTuple , Set , Tuple
20
+
21
+ if TYPE_CHECKING :
22
+ from _typeshed import StrPath
23
+
24
+ from typing_extensions import TypeAlias
20
25
21
26
import tomli
22
27
26
31
parser .add_argument ("-x" , "--exclude" , type = str , nargs = "*" , help = "Exclude pattern" )
27
32
parser .add_argument ("-p" , "--python-version" , type = str , nargs = "*" , help = "These versions only (major[.minor])" )
28
33
parser .add_argument ("--platform" , help = "Run mypy for a certain OS platform (defaults to sys.platform)" )
29
- parser .add_argument (
30
- "--warn-unused-ignores" ,
31
- action = "store_true" ,
32
- help = "Run mypy with --warn-unused-ignores "
33
- "(hint: only get rid of warnings that are "
34
- "unused for all platforms and Python versions)" ,
35
- )
36
-
37
34
parser .add_argument ("filter" , type = str , nargs = "*" , help = "Include pattern (default all)" )
38
35
39
36
40
- def log (args , * varargs ) :
37
+ def log (args : argparse . Namespace , * varargs : object ) -> None :
41
38
if args .verbose >= 2 :
42
39
print (* varargs )
43
40
44
41
45
- def match (fn , args ) :
42
+ def match (fn : str , args : argparse . Namespace ) -> bool :
46
43
if not args .filter and not args .exclude :
47
44
log (args , fn , "accept by default" )
48
45
return True
@@ -64,9 +61,10 @@ def match(fn, args):
64
61
65
62
66
63
_VERSION_LINE_RE = re .compile (r"^([a-zA-Z_][a-zA-Z0-9_.]*): ([23]\.\d{1,2})-([23]\.\d{1,2})?$" )
64
+ VersionsTuple : TypeAlias = Tuple [int , int ]
67
65
68
66
69
- def parse_versions (fname ) :
67
+ def parse_versions (fname : "StrPath" ) -> Dict [ str , Tuple [ VersionsTuple , VersionsTuple ]] :
70
68
result = {}
71
69
with open (fname ) as f :
72
70
for line in f :
@@ -76,7 +74,7 @@ def parse_versions(fname):
76
74
continue
77
75
m = _VERSION_LINE_RE .match (line )
78
76
assert m , "invalid VERSIONS line: " + line
79
- mod = m .group (1 )
77
+ mod : str = m .group (1 )
80
78
min_version = parse_version (m .group (2 ))
81
79
max_version = parse_version (m .group (3 )) if m .group (3 ) else (99 , 99 )
82
80
result [mod ] = min_version , max_version
@@ -86,13 +84,13 @@ def parse_versions(fname):
86
84
_VERSION_RE = re .compile (r"^([23])\.(\d+)$" )
87
85
88
86
89
- def parse_version (v_str ) :
87
+ def parse_version (v_str : str ) -> Tuple [ int , int ] :
90
88
m = _VERSION_RE .match (v_str )
91
89
assert m , "invalid version: " + v_str
92
90
return int (m .group (1 )), int (m .group (2 ))
93
91
94
92
95
- def add_files (files , seen , root , name , args ) :
93
+ def add_files (files : List [ str ] , seen : Set [ str ] , root : str , name : str , args : argparse . Namespace ) -> None :
96
94
"""Add all files in package or module represented by 'name' located in 'root'."""
97
95
full = os .path .join (root , name )
98
96
mod , ext = os .path .splitext (name )
@@ -127,7 +125,7 @@ class MypyDistConf(NamedTuple):
127
125
# disallow_untyped_defs = true
128
126
129
127
130
- def add_configuration (configurations : list [MypyDistConf ], distribution : str ) -> None :
128
+ def add_configuration (configurations : List [MypyDistConf ], distribution : str ) -> None :
131
129
with open (os .path .join ("stubs" , distribution , "METADATA.toml" )) as f :
132
130
data = dict (tomli .loads (f .read ()))
133
131
@@ -150,7 +148,15 @@ def add_configuration(configurations: list[MypyDistConf], distribution: str) ->
150
148
configurations .append (MypyDistConf (module_name , values .copy ()))
151
149
152
150
153
- def run_mypy (args , configurations , major , minor , files , * , custom_typeshed = False ):
151
+ def run_mypy (
152
+ args : argparse .Namespace ,
153
+ configurations : List [MypyDistConf ],
154
+ major : int ,
155
+ minor : int ,
156
+ files : List [str ],
157
+ * ,
158
+ custom_typeshed : bool = False ,
159
+ ) -> int :
154
160
try :
155
161
from mypy .api import run as mypy_run
156
162
except ImportError :
@@ -178,7 +184,9 @@ def run_mypy(args, configurations, major, minor, files, *, custom_typeshed=False
178
184
return exit_code
179
185
180
186
181
- def get_mypy_flags (args , major : int , minor : int , temp_name : str , * , custom_typeshed : bool = False ) -> list [str ]:
187
+ def get_mypy_flags (
188
+ args : argparse .Namespace , major : int , minor : int , temp_name : str , * , custom_typeshed : bool = False
189
+ ) -> List [str ]:
182
190
flags = [
183
191
"--python-version" ,
184
192
"%d.%d" % (major , minor ),
@@ -200,14 +208,12 @@ def get_mypy_flags(args, major: int, minor: int, temp_name: str, *, custom_types
200
208
# Setting custom typeshed dir prevents mypy from falling back to its bundled
201
209
# typeshed in case of stub deletions
202
210
flags .extend (["--custom-typeshed-dir" , os .path .dirname (os .path .dirname (__file__ ))])
203
- if args .warn_unused_ignores :
204
- flags .append ("--warn-unused-ignores" )
205
211
if args .platform :
206
212
flags .extend (["--platform" , args .platform ])
207
213
return flags
208
214
209
215
210
- def read_dependencies (distribution : str ) -> list [str ]:
216
+ def read_dependencies (distribution : str ) -> List [str ]:
211
217
with open (os .path .join ("stubs" , distribution , "METADATA.toml" )) as f :
212
218
data = dict (tomli .loads (f .read ()))
213
219
requires = data .get ("requires" , [])
@@ -221,7 +227,12 @@ def read_dependencies(distribution: str) -> list[str]:
221
227
222
228
223
229
def add_third_party_files (
224
- distribution : str , major : int , files : list [str ], args , configurations : list [MypyDistConf ], seen_dists : set [str ]
230
+ distribution : str ,
231
+ major : int ,
232
+ files : List [str ],
233
+ args : argparse .Namespace ,
234
+ configurations : List [MypyDistConf ],
235
+ seen_dists : Set [str ],
225
236
) -> None :
226
237
if distribution in seen_dists :
227
238
return
@@ -240,16 +251,16 @@ def add_third_party_files(
240
251
add_configuration (configurations , distribution )
241
252
242
253
243
- def test_third_party_distribution (distribution : str , major : int , minor : int , args ) -> tuple [int , int ]:
254
+ def test_third_party_distribution (distribution : str , major : int , minor : int , args : argparse . Namespace ) -> Tuple [int , int ]:
244
255
"""Test the stubs of a third-party distribution.
245
256
246
257
Return a tuple, where the first element indicates mypy's return code
247
258
and the second element is the number of checked files.
248
259
"""
249
260
250
- files : list [str ] = []
251
- configurations : list [MypyDistConf ] = []
252
- seen_dists : set [str ] = set ()
261
+ files : List [str ] = []
262
+ configurations : List [MypyDistConf ] = []
263
+ seen_dists : Set [str ] = set ()
253
264
add_third_party_files (distribution , major , files , args , configurations , seen_dists )
254
265
255
266
print (f"testing { distribution } ({ len (files )} files)..." )
@@ -267,7 +278,7 @@ def is_probably_stubs_folder(distribution: str, distribution_path: Path) -> bool
267
278
return distribution != ".mypy_cache" and distribution_path .is_dir ()
268
279
269
280
270
- def main ():
281
+ def main () -> None :
271
282
args = parser .parse_args ()
272
283
273
284
versions = [(3 , 11 ), (3 , 10 ), (3 , 9 ), (3 , 8 ), (3 , 7 ), (3 , 6 ), (2 , 7 )]
@@ -285,7 +296,7 @@ def main():
285
296
seen = {"__builtin__" , "builtins" , "typing" } # Always ignore these.
286
297
287
298
# Test standard library files.
288
- files = []
299
+ files : List [ str ] = []
289
300
if major == 2 :
290
301
root = os .path .join ("stdlib" , "@python2" )
291
302
for name in os .listdir (root ):
0 commit comments