forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestsamples.py
More file actions
80 lines (63 loc) · 2.75 KB
/
testsamples.py
File metadata and controls
80 lines (63 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""Self check mypy package"""
import sys
import os.path
from typing import List, Set
from mypy.test.helpers import Suite, run_mypy
class TypeshedSuite(Suite):
def check_stubs(self, version: str, *directories: str) -> None:
if not directories:
directories = (version,)
for stub_type in ['stdlib', 'third_party']:
for dir in directories:
seen = {'__builtin__'} # we don't want to check __builtin__, as it causes problems
modules = []
stubdir = os.path.join('typeshed', stub_type, dir)
for f in find_files(stubdir, suffix='.pyi'):
module = file_to_module(f[len(stubdir) + 1:])
if module not in seen:
seen.add(module)
modules.extend(['-m', module])
if modules:
run_mypy(['--python-version={}'.format(version)] + modules)
def test_2(self) -> None:
self.check_stubs("2.7", "2", "2and3")
def test_3(self) -> None:
sys_ver_str = '.'.join(map(str, sys.version_info[:2]))
self.check_stubs(sys_ver_str, "3", "2and3")
def test_34(self) -> None:
self.check_stubs("3.4")
def test_35(self) -> None:
self.check_stubs("3.5")
def test_36(self) -> None:
self.check_stubs("3.6")
def test_37(self) -> None:
self.check_stubs("3.7")
class SamplesSuite(Suite):
def test_samples(self) -> None:
for f in find_files(os.path.join('test-data', 'samples'), suffix='.py'):
mypy_args = ['--no-strict-optional']
if f == os.path.join('test-data', 'samples', 'crawl2.py'):
# This test requires 3.5 for async functions
mypy_args.append('--python-version=3.5')
run_mypy(mypy_args + [f])
def test_stdlibsamples(self) -> None:
seen = set() # type: Set[str]
stdlibsamples_dir = os.path.join('test-data', 'stdlib-samples', '3.2', 'test')
modules = [] # type: List[str]
for f in find_files(stdlibsamples_dir, prefix='test_', suffix='.py'):
if f not in seen:
seen.add(f)
modules.append(f)
if modules:
# TODO: Remove need for --no-strict-optional
run_mypy(['--no-strict-optional', '--platform=linux'] + modules)
def find_files(base: str, prefix: str = '', suffix: str = '') -> List[str]:
return [os.path.join(root, f)
for root, dirs, files in os.walk(base)
for f in files
if f.startswith(prefix) and f.endswith(suffix)]
def file_to_module(file: str) -> str:
rv = os.path.splitext(file)[0].replace(os.sep, '.')
if rv.endswith('.__init__'):
rv = rv[:-len('.__init__')]
return rv