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

Skip to content

Commit 8eb1cc9

Browse files
fix version ranges (#16)
1 parent 850324e commit 8eb1cc9

6 files changed

Lines changed: 51 additions & 14 deletions

File tree

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
[tool.black]
2-
target_version = ['py36', 'py37', 'py38']
3-
include = '\.py$'
2+
target_version = ['py36', 'py37', 'py38', 'py39']
3+
include = '\.pyi?$'
4+
skip-magic-trailing-comma = true
5+
experimental-string-processing = true
46

57
exclude = '''
68
/(
79
\.git
810
| \.mypy_cache
911
| \.tox
1012
| \.venv
13+
| typeshed_client/typeshed
1114
)/
1215
'''
1316

tests/test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
import sys
23
from typed_ast import ast3
34
import typeshed_client
45
from typeshed_client.finder import (
@@ -45,6 +46,8 @@ def test_get_stub_file(self) -> None:
4546

4647
self.check("subdir", (3, 6), TEST_TYPESHED / "subdir/__init__.pyi")
4748
self.check("subdir.overloads", (3, 7), TEST_TYPESHED / "subdir/overloads.pyi")
49+
self.check("subdir", (2, 7), TEST_TYPESHED / "@python2/subdir.pyi")
50+
self.check("subdir.overloads", (2, 7), None)
4851

4952
def test_third_party(self) -> None:
5053
self.check("thirdparty", (3, 6), PACKAGES / "thirdparty-stubs/__init__.pyi")
@@ -57,8 +60,7 @@ def test_get_all_stub_files(self) -> None:
5760
{
5861
("thirdparty", PACKAGES / "thirdparty-stubs/__init__.pyi"),
5962
("nostubs", PACKAGES / "nostubs/__init__.pyi"),
60-
("subdir", TEST_TYPESHED / "subdir/__init__.pyi"),
61-
("subdir.overloads", TEST_TYPESHED / "subdir/overloads.pyi"),
63+
("subdir", TEST_TYPESHED / "@python2/subdir.pyi"),
6264
("py2only", TEST_TYPESHED / "@python2/py2only.pyi"),
6365
("lib", TEST_TYPESHED / "@python2/lib.pyi"),
6466
("conditions", TEST_TYPESHED / "conditions.pyi"),

tests/typeshed/@python2/subdir.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x: int

tests/typeshed/overloads.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import overload
2+
23
@overload
34
def overloaded(x: int) -> None: ...
45
@overload

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ envlist = py36,py37,py38,py39,black
55
[testenv]
66
deps =
77
typed_ast >= 1.0.3
8-
black == 20.8b1
8+
black == 21.5b0
99
commands =
1010
python tests/test.py
1111

typeshed_client/finder.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ def get_all_stub_files(
137137
module_name = entry.name[: -len(".pyi")]
138138
else:
139139
continue
140-
if search_context.version < versions[module_name]:
140+
version = versions[module_name]
141+
if search_context.version < version.min:
142+
continue
143+
if version.max is not None and search_context.version > version.max:
144+
continue
145+
if typeshed_dir.name != "@python2" and version.in_python2:
141146
continue
142147
if entry.is_dir():
143148
seen = yield from _get_all_stub_files_from_directory(
@@ -223,35 +228,60 @@ def get_stub_file_name(
223228

224229
# 5. typeshed
225230
versions = get_typeshed_versions(search_context.typeshed)
226-
if (
227-
top_level_name not in versions
228-
or search_context.version < versions[top_level_name]
229-
):
231+
if top_level_name not in versions:
232+
return None
233+
version = versions[top_level_name]
234+
if search_context.version < version.min:
235+
return None
236+
if version.max is not None and search_context.version > version.max:
230237
return None
231238

232239
if search_context.version[0] == 2:
233240
python2_dir = search_context.typeshed / "@python2"
234241
stub = _find_stub_in_dir(python2_dir, module_name)
235-
if stub is not None:
242+
if stub is not None or version.in_python2:
236243
return stub
237244

238245
return _find_stub_in_dir(search_context.typeshed, module_name)
239246

240247

248+
class _VersionData(NamedTuple):
249+
min: PythonVersion
250+
max: Optional[PythonVersion]
251+
# whether it is present in @python2
252+
in_python2: bool
253+
254+
241255
@lru_cache()
242-
def get_typeshed_versions(typeshed: Path) -> Dict[str, PythonVersion]:
256+
def get_typeshed_versions(typeshed: Path) -> Dict[str, _VersionData]:
243257
versions = {}
258+
python2_files = set(os.listdir(typeshed / "@python2"))
244259
with (typeshed / "VERSIONS").open() as f:
245260
for line in f:
246261
line = line.strip()
247262
if not line or line.startswith("#"):
248263
continue
249264
module, version = line.split(": ")
250-
major, minor = version.split(".")
251-
versions[module] = (int(major), int(minor))
265+
if "-" in version:
266+
min_version_str, max_version_str = version.split("-")
267+
else:
268+
min_version_str = version
269+
max_version_str = None
270+
if max_version_str:
271+
max_version = _parse_version(max_version_str)
272+
else:
273+
max_version = None
274+
min_version = _parse_version(min_version_str)
275+
python2_only = module in python2_files or module + ".pyi" in python2_files
276+
versions[module] = _VersionData(min_version, max_version, python2_only)
252277
return versions
253278

254279

280+
def _parse_version(version: str) -> PythonVersion:
281+
major, minor = version.split(".")
282+
return (int(major), int(minor))
283+
284+
255285
def _find_stub_in_dir(stubdir: Path, module: ModulePath) -> Optional[Path]:
256286
if not module:
257287
init_name = stubdir / "__init__.pyi"

0 commit comments

Comments
 (0)