Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef7528e

Browse files
Switch to a bundled typeshed (#12)
This will be necessary once mypy stops bundling all of typeshed in its next release. This also makes typeshed-client able to find other PEP 561 packages.
1 parent 990f792 commit ef7528e

703 files changed

Lines changed: 53912 additions & 251 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

flit.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ author = Jelle Zijlstra
44
author-email = [email protected]
55
home-page = https://github.com/JelleZijlstra/typeshed_client
66
requires =
7-
mypy >= 0.501
8-
mypy_extensions >= 0.1.0
97
typed_ast >= 1.0.3
108
dev-requires = tox
119
description-file = README.rst

pyproject.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[tool.black]
2+
target_version = ['py36', 'py37', 'py38']
3+
include = '\.py$'
4+
5+
exclude = '''
6+
/(
7+
\.git
8+
| \.mypy_cache
9+
| \.tox
10+
| \.venv
11+
)/
12+
'''

setup.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ast
2+
import os
23
from pathlib import Path
34
import re
45
import sys
@@ -13,16 +14,27 @@
1314

1415

1516
current_dir = Path(__file__).parent.resolve()
17+
ts_client_dir = current_dir / "typeshed_client"
18+
typeshed_dir = ts_client_dir / "typeshed"
1619

1720

1821
_version_re = re.compile(r"__version__\s+=\s+(?P<version>.*)")
1922

2023

21-
with (current_dir / "typeshed_client/__init__.py").open() as f:
24+
with (ts_client_dir / "__init__.py").open() as f:
2225
version = _version_re.search(f.read()).group("version")
2326
version = str(ast.literal_eval(version))
2427

2528

29+
def find_bundled_files():
30+
for root, _, files in os.walk(typeshed_dir):
31+
root_path = Path(root)
32+
for file in files:
33+
path = root_path / file
34+
if path.suffix == ".pyi" or path.name == "VERSIONS":
35+
yield str(path)
36+
37+
2638
setup(
2739
name="typeshed_client",
2840
version=version,
@@ -34,11 +46,10 @@
3446
license="MIT",
3547
packages=["typeshed_client"],
3648
install_requires=[
37-
"mypy >= 0.740",
38-
"mypy_extensions >= 0.1.0",
3949
"typed_ast >= 1.0.3",
40-
"importlib_resources == 1.4.0",
50+
"importlib_resources >= 1.4.0",
4151
],
52+
package_data={"typeshed_client": list(find_bundled_files())},
4253
classifiers=[
4354
"Development Status :: 3 - Alpha",
4455
"Environment :: Console",
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/test.py

Lines changed: 64 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,76 @@
1-
from functools import partial
2-
import os
31
from pathlib import Path
42
from typed_ast import ast3
53
import 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
712
from unittest import mock
813
import unittest
914

1015
TEST_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

1628
class 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

8570
class 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

230220
class 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:
247237
class 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

271250
if __name__ == "__main__":

tests/typeshed/README

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)