@@ -27,11 +27,42 @@ def use_tmp_dir() -> Iterator[None]:
2727
2828TEST_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
3160def 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
6296def 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\n temp: 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" ]))
0 commit comments