17
17
18
18
import argparse
19
19
import collections
20
+ import itertools
20
21
import os
21
22
import re
22
23
import subprocess
27
28
help = "Don't actually run tests" )
28
29
parser .add_argument ('--num-parallel' , type = int , default = 1 ,
29
30
help = 'Number of test processes to spawn' )
30
- # Default to '' so that symlinking typeshed/stdlib in cwd will work.
31
+ # Default to '' so that symlinking typeshed subdirs in cwd will work.
31
32
parser .add_argument ('--typeshed-location' , type = str , default = '' ,
32
33
help = 'Path to typeshed installation.' )
33
34
# Default to '' so that finding pytype in path will work.
44
45
Dirs = collections .namedtuple ('Dirs' , ['pytype' , 'typeshed' ])
45
46
46
47
48
+ TYPESHED_SUBDIRS = ['stdlib' , 'third_party' ]
49
+
50
+
47
51
def main ():
48
52
args = parser .parse_args ()
49
53
code , runs = pytype_test (args )
@@ -123,12 +127,19 @@ def communicate(self):
123
127
124
128
125
129
def _get_relative (filename ):
126
- top = filename .find ('stdlib/' )
130
+ top = 0
131
+ for d in TYPESHED_SUBDIRS :
132
+ try :
133
+ top = filename .index (d )
134
+ except ValueError :
135
+ continue
136
+ else :
137
+ break
127
138
return filename [top :]
128
139
129
140
130
141
def _get_module_name (filename ):
131
- """Converts a filename stdlib /m.n/module/foo to module.foo."""
142
+ """Converts a filename {subdir} /m.n/module/foo to module.foo."""
132
143
return '.' .join (_get_relative (filename ).split (os .path .sep )[2 :]).replace (
133
144
'.pyi' , '' ).replace ('.__init__' , '' )
134
145
@@ -142,15 +153,20 @@ def can_run(path, exe, *args):
142
153
return False
143
154
144
155
156
+ def _is_version (path , version ):
157
+ return any ('%s/%s' % (d , version ) in path for d in TYPESHED_SUBDIRS )
158
+
159
+
145
160
def pytype_test (args ):
146
161
dirs = get_project_dirs (args )
147
162
pytype_exe = os .path .join (dirs .pytype , 'pytype-single' )
148
- stdlib_path = os .path .join (dirs .typeshed , 'stdlib' )
163
+ paths = [ os .path .join (dirs .typeshed , d ) for d in TYPESHED_SUBDIRS ]
149
164
150
- if not os .path .isdir (stdlib_path ):
151
- print ('Cannot find typeshed stdlib at %s '
152
- '(specify parent dir via --typeshed_location)' % stdlib_path )
153
- return 0 , 0
165
+ for p in paths :
166
+ if not os .path .isdir (p ):
167
+ print ('Cannot find typeshed subdir at %s '
168
+ '(specify parent dir via --typeshed_location)' % p )
169
+ return 0 , 0
154
170
155
171
if can_run (dirs .pytype , 'pytd' , '-h' ):
156
172
pytd_exe = os .path .join (dirs .pytype , 'pytd' )
@@ -162,10 +178,14 @@ def pytype_test(args):
162
178
163
179
if not can_run ('' , args .python36_exe , '--version' ):
164
180
print ('Cannot run python3.6 from %s. (point to a valid executable via '
165
- '--python36_exe )' % args .python36_exe )
181
+ '--python36-exe )' % args .python36_exe )
166
182
return 0 , 0
167
183
168
- wanted = re .compile (r'stdlib/.*\.pyi$' )
184
+ stdlib = 'stdlib/'
185
+ six = 'third_party/.*/six/'
186
+ mypy_extensions = 'third_party/.*/mypy_extensions'
187
+ wanted = re .compile (
188
+ r'(?:%s).*\.pyi$' % '|' .join ([stdlib , six , mypy_extensions ]))
169
189
skip , parse_only = load_blacklist (dirs )
170
190
skipped = PathMatcher (skip )
171
191
parse_only = PathMatcher (parse_only )
@@ -189,7 +209,8 @@ def _make_test(filename, major_version):
189
209
dry_run = args .dry_run ,
190
210
env = {"TYPESHED_HOME" : dirs .typeshed })
191
211
192
- for root , _ , filenames in os .walk (stdlib_path ):
212
+ for root , _ , filenames in itertools .chain .from_iterable (
213
+ os .walk (p ) for p in paths ):
193
214
for f in sorted (filenames ):
194
215
f = os .path .join (root , f )
195
216
rel = _get_relative (f )
@@ -203,22 +224,22 @@ def _make_test(filename, major_version):
203
224
max_code , runs , errors = 0 , 0 , 0
204
225
files = pytype_run + pytd_run
205
226
total_tests = len (files )
206
- # Files in stdlib /2and3 get tested twice
207
- total_tests += sum (1 for f in pytype_run if 'stdlib/ 2and3' in f )
227
+ # Files in {subdir} /2and3 get tested twice
228
+ total_tests += sum (1 for f in pytype_run if _is_version ( f , ' 2and3') )
208
229
print ("Testing files with pytype..." )
209
230
while 1 :
210
231
while files and len (running_tests ) < args .num_parallel :
211
232
f = files .pop ()
212
233
if f in pytype_run :
213
- if 'stdlib/ 2and3' in f :
234
+ if _is_version ( f , ' 2and3') :
214
235
running_tests .append (_make_test (f , 2 ))
215
236
running_tests .append (_make_test (f , 3 ))
216
- elif 'stdlib/2' in f :
237
+ elif _is_version ( f , '2' ) :
217
238
running_tests .append (_make_test (f , 2 ))
218
- elif 'stdlib/3' in f :
239
+ elif _is_version ( f , '3' ) :
219
240
running_tests .append (_make_test (f , 3 ))
220
241
else :
221
- print ("Unrecognised stdlib path: %s" % f )
242
+ print ("Unrecognised path: %s" % f )
222
243
elif f in pytd_run :
223
244
test_run = BinaryRun ([pytd_exe , f ], dry_run = args .dry_run )
224
245
running_tests .append (test_run )
0 commit comments