Skip to content

Commit 3bcc8bf

Browse files
emmatypingMichael0x2a
authored andcommitted
Improve stub testing to cover all of typeshed (#5051)
Improve stub testing to cover all of typeshed across all supported Python versions.
1 parent c13d6f1 commit 3bcc8bf

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

mypy/test/testsamples.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,45 @@
66
from mypy.test.helpers import Suite, run_mypy
77

88

9-
class SamplesSuite(Suite):
10-
def test_stubs(self) -> None:
11-
# We only test each module in the one version mypy prefers to find.
12-
# TODO: test stubs for other versions, especially Python 2 stubs.
13-
seen = set() # type: Set[str]
14-
modules = []
15-
# TODO: This should also test Python 2, and pass pyversion accordingly.
16-
for version in ["2and3", "3", "3.5"]:
17-
# FIX: remove 'builtins', this directory does not exist
18-
for stub_type in ['builtins', 'stdlib', 'third_party']:
19-
stubdir = os.path.join('typeshed', stub_type, version)
9+
class TypeshedSuite(Suite):
10+
def check_stubs(self, version: str, *directories: str) -> None:
11+
if not directories:
12+
directories = (version,)
13+
for stub_type in ['stdlib', 'third_party']:
14+
for dir in directories:
15+
seen = {'__builtin__'} # we don't want to check __builtin__, as it causes problems
16+
modules = []
17+
stubdir = os.path.join('typeshed', stub_type, dir)
2018
for f in find_files(stubdir, suffix='.pyi'):
2119
module = file_to_module(f[len(stubdir) + 1:])
2220
if module not in seen:
2321
seen.add(module)
2422
modules.extend(['-m', module])
25-
if modules:
26-
# these require at least 3.5 otherwise it will fail trying to import zipapp
27-
run_mypy(['--python-version=3.5'] + modules)
2823

24+
if modules:
25+
run_mypy(['--python-version={}'.format(version)] + modules)
26+
27+
def test_2(self) -> None:
28+
self.check_stubs("2.7", "2", "2and3")
29+
30+
def test_3(self) -> None:
31+
sys_ver_str = '.'.join(map(str, sys.version_info[:2]))
32+
self.check_stubs(sys_ver_str, "3", "2and3")
33+
34+
def test_34(self) -> None:
35+
self.check_stubs("3.4")
36+
37+
def test_35(self) -> None:
38+
self.check_stubs("3.5")
39+
40+
def test_36(self) -> None:
41+
self.check_stubs("3.6")
42+
43+
def test_37(self) -> None:
44+
self.check_stubs("3.7")
45+
46+
47+
class SamplesSuite(Suite):
2948
def test_samples(self) -> None:
3049
for f in find_files(os.path.join('test-data', 'samples'), suffix='.py'):
3150
mypy_args = ['--no-strict-optional']

0 commit comments

Comments
 (0)