Skip to content

Commit ecec8bb

Browse files
authored
stubtest: speed up tests by a lot (python#9621)
I probably should have done this earlier...
1 parent 6bbc8c5 commit ecec8bb

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

mypy/stubtest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ def strip_comments(s: str) -> str:
10221022
yield entry
10231023

10241024

1025-
def test_stubs(args: argparse.Namespace) -> int:
1025+
def test_stubs(args: argparse.Namespace, use_builtins_fixtures: bool = False) -> int:
10261026
"""This is stubtest! It's time to test the stubs!"""
10271027
# Load the allowlist. This is a series of strings corresponding to Error.object_desc
10281028
# Values in the dict will store whether we used the allowlist entry or not.
@@ -1049,6 +1049,7 @@ def test_stubs(args: argparse.Namespace) -> int:
10491049
options.incremental = False
10501050
options.custom_typeshed_dir = args.custom_typeshed_dir
10511051
options.config_file = args.mypy_config_file
1052+
options.use_builtins_fixtures = use_builtins_fixtures
10521053

10531054
if options.config_file:
10541055
def set_strict_flags() -> None: # not needed yet

mypy/test/teststubtest.py

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,42 @@ def use_tmp_dir() -> Iterator[None]:
2727

2828
TEST_MODULE_NAME = "test_module"
2929

30+
stubtest_builtins_stub = """
31+
from typing import Generic, Mapping, Sequence, TypeVar, overload
32+
33+
T = TypeVar('T')
34+
T_co = TypeVar('T_co', covariant=True)
35+
KT = TypeVar('KT')
36+
VT = TypeVar('VT')
37+
38+
class object:
39+
def __init__(self) -> None: pass
40+
class type: ...
41+
42+
class tuple(Sequence[T_co], Generic[T_co]): ...
43+
class dict(Mapping[KT, VT]): ...
44+
45+
class function: pass
46+
class ellipsis: pass
47+
48+
class int: ...
49+
class float: ...
50+
class bool(int): ...
51+
class str: ...
52+
class bytes: ...
53+
54+
def property(f: T) -> T: ...
55+
def classmethod(f: T) -> T: ...
56+
def staticmethod(f: T) -> T: ...
57+
"""
58+
3059

3160
def run_stubtest(
3261
stub: str, runtime: str, options: List[str], config_file: Optional[str] = None,
3362
) -> str:
3463
with use_tmp_dir():
64+
with open("builtins.pyi", "w") as f:
65+
f.write(stubtest_builtins_stub)
3566
with open("{}.pyi".format(TEST_MODULE_NAME), "w") as f:
3667
f.write(stub)
3768
with open("{}.py".format(TEST_MODULE_NAME), "w") as f:
@@ -47,7 +78,10 @@ def run_stubtest(
4778

4879
output = io.StringIO()
4980
with contextlib.redirect_stdout(output):
50-
test_stubs(parse_options([TEST_MODULE_NAME] + options))
81+
test_stubs(
82+
parse_options([TEST_MODULE_NAME] + options),
83+
use_builtins_fixtures=True
84+
)
5185

5286
return output.getvalue()
5387

@@ -60,10 +94,10 @@ def __init__(self, stub: str, runtime: str, error: Optional[str]):
6094

6195

6296
def collect_cases(fn: Callable[..., Iterator[Case]]) -> Callable[..., None]:
63-
"""Repeatedly invoking run_stubtest is slow, so use this decorator to combine cases.
97+
"""run_stubtest used to be slow, so we used this decorator to combine cases.
6498
65-
We could also manually combine cases, but this allows us to keep the contrasting stub and
66-
runtime definitions next to each other.
99+
If you're reading this and bored, feel free to refactor this and make it more like
100+
other mypy tests.
67101
68102
"""
69103

@@ -775,12 +809,6 @@ def f(a: int, b: int, *, c: int, d: int = 0, **kwargs: Any) -> None:
775809
== "def (a, b, *, c, d = ..., **kwargs)"
776810
)
777811

778-
779-
class StubtestIntegration(unittest.TestCase):
780-
def test_typeshed(self) -> None:
781-
# check we don't crash while checking typeshed
782-
test_stubs(parse_options(["--check-typeshed"]))
783-
784812
def test_config_file(self) -> None:
785813
runtime = "temp = 5\n"
786814
stub = "from decimal import Decimal\ntemp: Decimal\n"
@@ -795,3 +823,9 @@ def test_config_file(self) -> None:
795823
)
796824
output = run_stubtest(stub=stub, runtime=runtime, options=[], config_file=config_file)
797825
assert output == ""
826+
827+
828+
class StubtestIntegration(unittest.TestCase):
829+
def test_typeshed(self) -> None:
830+
# check we don't crash while checking typeshed
831+
test_stubs(parse_options(["--check-typeshed"]))

runtests.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
MYPYC_EXTERNAL = 'TestExternal'
2929
MYPYC_COMMAND_LINE = 'TestCommandLine'
3030
ERROR_STREAM = 'ErrorStreamSuite'
31-
STUBTEST = 'StubtestUnit'
32-
STUBTEST_MISC = 'StubtestMiscUnit'
3331
STUBTEST_INTEGRATION = 'StubtestIntegration'
3432

3533

@@ -47,18 +45,15 @@
4745
MYPYC_EXTERNAL,
4846
MYPYC_COMMAND_LINE,
4947
ERROR_STREAM,
50-
STUBTEST,
51-
STUBTEST_MISC,
5248
STUBTEST_INTEGRATION,
5349
]
5450

5551

5652
# These must be enabled by explicitly including 'mypyc-extra' on the command line.
57-
MYPYC_OPT_IN = [MYPYC_RUN,
58-
MYPYC_RUN_MULTI]
53+
MYPYC_OPT_IN = [MYPYC_RUN, MYPYC_RUN_MULTI]
5954

6055
# These must be enabled by explicitly including 'stubtest' on the command line.
61-
STUBTEST_OPT_IN = [STUBTEST, STUBTEST_MISC, STUBTEST_INTEGRATION]
56+
STUBTEST_OPT_IN = [STUBTEST_INTEGRATION]
6257

6358
# We split the pytest run into three parts to improve test
6459
# parallelization. Each run should have tests that each take a roughly similar

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy