1- from functools import partial
2- import os
31from pathlib import Path
42from typed_ast import ast3
53import typeshed_client
6- from typing import Any , Set , Tuple , Type
4+ from typeshed_client .finder import (
5+ get_search_context ,
6+ PythonVersion ,
7+ get_stub_file ,
8+ SearchContext ,
9+ )
10+ from typeshed_client .parser import get_stub_names
11+ from typing import Any , Set , Type , Optional
712from unittest import mock
813import unittest
914
1015TEST_TYPESHED = Path (__file__ ).parent / "typeshed"
16+ PACKAGES = Path (__file__ ).parent / "site-packages"
1117
12- get_stub_file = partial (typeshed_client .get_stub_file , typeshed_dir = TEST_TYPESHED )
13- get_stub_names = partial (typeshed_client .get_stub_names , typeshed_dir = TEST_TYPESHED )
18+
19+ def get_context (version : PythonVersion , platform : str = "linux" ) -> SearchContext :
20+ return get_search_context (
21+ version = version ,
22+ typeshed = TEST_TYPESHED ,
23+ search_path = [PACKAGES ],
24+ platform = platform ,
25+ )
1426
1527
1628class TestFinder (unittest .TestCase ):
29+ def check (
30+ self , name : str , version : PythonVersion , expected : Optional [Path ]
31+ ) -> None :
32+ ctx = get_context (version )
33+ self .assertEqual (get_stub_file (name , search_context = ctx ), expected )
34+
1735 def test_get_stub_file (self ) -> None :
18- self .assertEqual (get_stub_file ("lib" ), TEST_TYPESHED / "stdlib/3.5/lib.pyi" )
19- self .assertEqual (
20- get_stub_file ("lib" , version = (3 , 6 )), TEST_TYPESHED / "stdlib/3.5/lib.pyi"
21- )
22- self .assertEqual (
23- get_stub_file ("lib" , version = (3 , 5 )), TEST_TYPESHED / "stdlib/3.5/lib.pyi"
24- )
25- self .assertEqual (
26- get_stub_file ("lib" , version = (3 , 4 )), TEST_TYPESHED / "stdlib/3.4/lib.pyi"
27- )
28- self .assertEqual (
29- get_stub_file ("lib" , version = (3 , 3 )), TEST_TYPESHED / "stdlib/3.3/lib.pyi"
30- )
31- self .assertEqual (
32- get_stub_file ("lib" , version = (3 , 2 )), TEST_TYPESHED / "stdlib/3/lib.pyi"
33- )
34- self .assertEqual (
35- get_stub_file ("lib" , version = (2 , 7 )), TEST_TYPESHED / "stdlib/2/lib.pyi"
36- )
37- self .assertEqual (
38- get_stub_file ("subdir.overloads" , version = (3 , 6 )),
39- TEST_TYPESHED / "stdlib/3/subdir/overloads.pyi" ,
40- )
41- self .assertEqual (
42- get_stub_file ("overloads" , version = (3 , 6 )),
43- TEST_TYPESHED / "stdlib/3/overloads.pyi" ,
44- )
45- for version in ((2 , 7 ), (3 , 3 ), (3 , 4 ), (3 , 5 ), (3 , 6 )):
46- self .assertEqual (
47- get_stub_file ("shared" , version = version ),
48- TEST_TYPESHED / "stdlib/2and3/shared.pyi" ,
49- )
36+ self .check ("lib" , (3 , 6 ), TEST_TYPESHED / "lib.pyi" )
37+ self .check ("lib" , (3 , 5 ), TEST_TYPESHED / "lib.pyi" )
38+ self .check ("lib" , (2 , 7 ), TEST_TYPESHED / "@python2/lib.pyi" )
5039
51- def test_third_party (self ) -> None :
52- self .assertEqual (
53- get_stub_file ("thirdpart" , version = (3 , 5 )),
54- TEST_TYPESHED / "third_party/3.5/thirdpart.pyi" ,
55- )
56- self .assertEqual (
57- get_stub_file ("thirdpart" , version = (3 , 4 )),
58- TEST_TYPESHED / "stdlib/3.4/thirdpart.pyi" ,
59- )
40+ self .check ("py2only" , (3 , 5 ), None )
41+ self .check ("py2only" , (2 , 7 ), TEST_TYPESHED / "@python2/py2only.pyi" )
6042
61- def test_subdir (self ) -> None :
62- self .assertEqual (
63- get_stub_file ("subdir" , version = (3 , 5 )),
64- TEST_TYPESHED / "stdlib/3/subdir/__init__.pyi" ,
65- )
43+ self .check ("new37" , (3 , 6 ), None )
44+ self .check ("new37" , (3 , 7 ), TEST_TYPESHED / "new37.pyi" )
45+
46+ self .check ("subdir" , (3 , 6 ), TEST_TYPESHED / "subdir/__init__.pyi" )
47+ self .check ("subdir.overloads" , (3 , 7 ), TEST_TYPESHED / "subdir/overloads.pyi" )
48+
49+ def test_third_party (self ) -> None :
50+ self .check ("thirdparty" , (3 , 6 ), PACKAGES / "thirdparty-stubs/__init__.pyi" )
51+ self .check ("nostubs" , (3 , 6 ), PACKAGES / "nostubs/__init__.pyi" )
6652
6753 def test_get_all_stub_files (self ) -> None :
68- all_stubs = typeshed_client .get_all_stub_files (
69- version = (2 , 7 ), typeshed_dir = TEST_TYPESHED
70- )
54+ all_stubs = typeshed_client .get_all_stub_files (get_context ((2 , 7 )))
7155 self .assertEqual (
7256 set (all_stubs ),
7357 {
74- ("lib" , TEST_TYPESHED / "stdlib/2/lib.pyi" ),
75- ("conditions" , TEST_TYPESHED / "stdlib/2and3/conditions.pyi" ),
76- ("shared" , TEST_TYPESHED / "stdlib/2and3/shared.pyi" ),
77- (
78- "top_level_assert" ,
79- TEST_TYPESHED / "stdlib/2and3/top_level_assert.pyi" ,
80- ),
58+ ("thirdparty" , PACKAGES / "thirdparty-stubs/__init__.pyi" ),
59+ ("nostubs" , PACKAGES / "nostubs/__init__.pyi" ),
60+ ("subdir" , TEST_TYPESHED / "subdir/__init__.pyi" ),
61+ ("subdir.overloads" , TEST_TYPESHED / "subdir/overloads.pyi" ),
62+ ("py2only" , TEST_TYPESHED / "@python2/py2only.pyi" ),
63+ ("lib" , TEST_TYPESHED / "@python2/lib.pyi" ),
64+ ("conditions" , TEST_TYPESHED / "conditions.pyi" ),
65+ ("top_level_assert" , TEST_TYPESHED / "top_level_assert.pyi" ),
8166 },
8267 )
8368
8469
8570class TestParser (unittest .TestCase ):
8671 def test_get_stub_names (self ) -> None :
87- names = get_stub_names ("simple" , version = (3 , 5 ))
72+ ctx = get_context ((3 , 5 ))
73+ names = get_stub_names ("simple" , search_context = ctx )
8874 self .assertEqual (
8975 set (names .keys ()),
9076 {
@@ -151,7 +137,8 @@ def test_get_stub_names(self) -> None:
151137 self .check_nameinfo (cls_names , "method" , ast3 .FunctionDef )
152138
153139 def test_starimport (self ) -> None :
154- names = get_stub_names ("starimport" , version = (3 , 5 ))
140+ ctx = get_context ((3 , 5 ))
141+ names = get_stub_names ("starimport" , search_context = ctx )
155142 self .assertEqual (set (names .keys ()), {"public" })
156143 self .check_nameinfo (names , "public" , typeshed_client .ImportedName )
157144 path = typeshed_client .ModulePath (("imported" ,))
@@ -205,20 +192,23 @@ def check_conditions(
205192 self ,
206193 names : Set [str ],
207194 * ,
208- version : Tuple [ int , int ] = (3 , 6 ),
195+ version : PythonVersion = (3 , 6 ),
209196 platform : str = "linux" ,
210197 ) -> None :
211- info = get_stub_names ("conditions" , version = version , platform = platform )
198+ ctx = get_context (version , platform )
199+ info = get_stub_names ("conditions" , search_context = ctx )
212200 self .assertEqual (set (info .keys ()), names | {"sys" })
213201
214202 def test_top_level_assert (self ):
215- info = get_stub_names ("top_level_assert" , version = (3 , 6 ), platform = "flat" )
203+ ctx = get_context ((3 , 6 ), "flat" )
204+ info = get_stub_names ("top_level_assert" , search_context = ctx )
216205 self .assertEqual (set (info .keys ()), set ())
217- info = get_stub_names ("top_level_assert" , version = (3 , 6 ), platform = "linux" )
206+ ctx = get_context ((3 , 6 ), "linux" )
207+ info = get_stub_names ("top_level_assert" , search_context = ctx )
218208 self .assertEqual (set (info .keys ()), {"x" , "sys" })
219209
220210 def test_overloads (self ) -> None :
221- names = get_stub_names ("overloads" , version = ( 3 , 5 ))
211+ names = get_stub_names ("overloads" , search_context = get_context (( 3 , 5 ) ))
222212 self .assertEqual (set (names .keys ()), {"overload" , "overloaded" })
223213 self .check_nameinfo (names , "overloaded" , typeshed_client .OverloadedName )
224214 definitions = names ["overloaded" ].ast .definitions
@@ -229,7 +219,7 @@ def test_overloads(self) -> None:
229219
230220class TestResolver (unittest .TestCase ):
231221 def test_simple (self ) -> None :
232- res = typeshed_client .Resolver (version = ( 3 , 5 ), typeshed_dir = TEST_TYPESHED )
222+ res = typeshed_client .Resolver (get_context (( 3 , 5 )) )
233223 path = typeshed_client .ModulePath (("simple" ,))
234224 other_path = typeshed_client .ModulePath (("other" ,))
235225
@@ -247,25 +237,14 @@ def test_simple(self) -> None:
247237class IntegrationTest (unittest .TestCase ):
248238 """Tests that all files in typeshed are parsed without error."""
249239
250- fake_env = typeshed_client .parser .Env (
251- (3 , 6 ), "linux" , typeshed_client .finder .find_typeshed ()
252- )
253240 fake_path = typeshed_client .ModulePath (("some" , "module" ))
254241
255242 def test (self ):
256- typeshed_root = typeshed_client .finder .find_typeshed ()
257- self .assertTrue (typeshed_root .is_dir ())
258- for dirpath , _ , filenames in os .walk (typeshed_root ):
259- for filename in filenames :
260- path = Path (dirpath ) / filename
261- if path .suffix != ".pyi" :
262- continue
263- with self .subTest (path = path ):
264- self .check_path (path )
265-
266- def check_path (self , path : Path ) -> None :
267- ast = typeshed_client .finder .parse_stub_file (path )
268- typeshed_client .parser .parse_ast (ast , self .fake_env , self .fake_path )
243+ ctx = get_search_context ()
244+ for module_name , module_path in typeshed_client .get_all_stub_files (ctx ):
245+ with self .subTest (path = module_name ):
246+ ast = typeshed_client .get_stub_ast (module_name , search_context = ctx )
247+ typeshed_client .parser .parse_ast (ast , ctx , self .fake_path )
269248
270249
271250if __name__ == "__main__" :
0 commit comments