@@ -27,11 +27,42 @@ def use_tmp_dir() -> Iterator[None]:
27
27
28
28
TEST_MODULE_NAME = "test_module"
29
29
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
+
30
59
31
60
def run_stubtest (
32
61
stub : str , runtime : str , options : List [str ], config_file : Optional [str ] = None ,
33
62
) -> str :
34
63
with use_tmp_dir ():
64
+ with open ("builtins.pyi" , "w" ) as f :
65
+ f .write (stubtest_builtins_stub )
35
66
with open ("{}.pyi" .format (TEST_MODULE_NAME ), "w" ) as f :
36
67
f .write (stub )
37
68
with open ("{}.py" .format (TEST_MODULE_NAME ), "w" ) as f :
@@ -47,7 +78,10 @@ def run_stubtest(
47
78
48
79
output = io .StringIO ()
49
80
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
+ )
51
85
52
86
return output .getvalue ()
53
87
@@ -60,10 +94,10 @@ def __init__(self, stub: str, runtime: str, error: Optional[str]):
60
94
61
95
62
96
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.
64
98
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 .
67
101
68
102
"""
69
103
@@ -775,12 +809,6 @@ def f(a: int, b: int, *, c: int, d: int = 0, **kwargs: Any) -> None:
775
809
== "def (a, b, *, c, d = ..., **kwargs)"
776
810
)
777
811
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
-
784
812
def test_config_file (self ) -> None :
785
813
runtime = "temp = 5\n "
786
814
stub = "from decimal import Decimal\n temp: Decimal\n "
@@ -795,3 +823,9 @@ def test_config_file(self) -> None:
795
823
)
796
824
output = run_stubtest (stub = stub , runtime = runtime , options = [], config_file = config_file )
797
825
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" ]))
0 commit comments