Skip to content

Commit ff821f9

Browse files
rchen152JelleZijlstra
authored andcommitted
Update pytype_test and pytype_blacklist (#2225)
* Add parts of third_party/ to pytype_test. Pytype uses six and mypy_extensions, so these pyi files should be tested with pytype. * Remove newly loadable pyi files from pytype_blacklist.
1 parent 451deba commit ff821f9

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

tests/pytype_blacklist.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,3 @@ stdlib/2/os/__init__.pyi # parse only
1515
# pytype doesn't yet support aliases with implicit type parameters
1616
# (e.g., here, FutureT = Future[T])
1717
stdlib/3.4/asyncio/tasks.pyi
18-
19-
# aliases to class constants
20-
stdlib/3/signal.pyi # parse only
21-
stdlib/3/re.pyi # parse only
22-
stdlib/2and3/plistlib.pyi # parse only

tests/pytype_test.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import argparse
1919
import collections
20+
import itertools
2021
import os
2122
import re
2223
import subprocess
@@ -27,7 +28,7 @@
2728
help="Don't actually run tests")
2829
parser.add_argument('--num-parallel', type=int, default=1,
2930
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.
3132
parser.add_argument('--typeshed-location', type=str, default='',
3233
help='Path to typeshed installation.')
3334
# Default to '' so that finding pytype in path will work.
@@ -44,6 +45,9 @@
4445
Dirs = collections.namedtuple('Dirs', ['pytype', 'typeshed'])
4546

4647

48+
TYPESHED_SUBDIRS = ['stdlib', 'third_party']
49+
50+
4751
def main():
4852
args = parser.parse_args()
4953
code, runs = pytype_test(args)
@@ -123,12 +127,19 @@ def communicate(self):
123127

124128

125129
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
127138
return filename[top:]
128139

129140

130141
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."""
132143
return '.'.join(_get_relative(filename).split(os.path.sep)[2:]).replace(
133144
'.pyi', '').replace('.__init__', '')
134145

@@ -142,15 +153,20 @@ def can_run(path, exe, *args):
142153
return False
143154

144155

156+
def _is_version(path, version):
157+
return any('%s/%s' % (d, version) in path for d in TYPESHED_SUBDIRS)
158+
159+
145160
def pytype_test(args):
146161
dirs = get_project_dirs(args)
147162
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]
149164

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
154170

155171
if can_run(dirs.pytype, 'pytd', '-h'):
156172
pytd_exe = os.path.join(dirs.pytype, 'pytd')
@@ -162,10 +178,14 @@ def pytype_test(args):
162178

163179
if not can_run('', args.python36_exe, '--version'):
164180
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)
166182
return 0, 0
167183

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]))
169189
skip, parse_only = load_blacklist(dirs)
170190
skipped = PathMatcher(skip)
171191
parse_only = PathMatcher(parse_only)
@@ -189,7 +209,8 @@ def _make_test(filename, major_version):
189209
dry_run=args.dry_run,
190210
env={"TYPESHED_HOME": dirs.typeshed})
191211

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):
193214
for f in sorted(filenames):
194215
f = os.path.join(root, f)
195216
rel = _get_relative(f)
@@ -203,22 +224,22 @@ def _make_test(filename, major_version):
203224
max_code, runs, errors = 0, 0, 0
204225
files = pytype_run + pytd_run
205226
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'))
208229
print("Testing files with pytype...")
209230
while 1:
210231
while files and len(running_tests) < args.num_parallel:
211232
f = files.pop()
212233
if f in pytype_run:
213-
if 'stdlib/2and3' in f:
234+
if _is_version(f, '2and3'):
214235
running_tests.append(_make_test(f, 2))
215236
running_tests.append(_make_test(f, 3))
216-
elif 'stdlib/2' in f:
237+
elif _is_version(f, '2'):
217238
running_tests.append(_make_test(f, 2))
218-
elif 'stdlib/3' in f:
239+
elif _is_version(f, '3'):
219240
running_tests.append(_make_test(f, 3))
220241
else:
221-
print("Unrecognised stdlib path: %s" % f)
242+
print("Unrecognised path: %s" % f)
222243
elif f in pytd_run:
223244
test_run = BinaryRun([pytd_exe, f], dry_run=args.dry_run)
224245
running_tests.append(test_run)

0 commit comments

Comments
 (0)