Skip to content

Commit 83c2518

Browse files
authored
Refactor VERSIONS handling (#16020)
`parse_stdlib_versions_file` now returns a class with methods `supported_versions_for_module` and `is_supported`, obsoleting stand-alone function `supported_versions_for_module`.
1 parent d80e8b7 commit 83c2518

4 files changed

Lines changed: 23 additions & 20 deletions

File tree

lib/ts_utils/utils.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,30 @@ def get_mypy_req() -> str:
149149
# ====================================================================
150150

151151
VersionTuple: TypeAlias = tuple[int, int]
152-
SupportedVersionsDict: TypeAlias = dict[str, tuple[VersionTuple, VersionTuple]]
153152

154153
VERSIONS_PATH = STDLIB_PATH / "VERSIONS"
155154
VERSION_LINE_RE = re.compile(r"^([a-zA-Z_][a-zA-Z0-9_.]*): ([23]\.\d{1,2})-([23]\.\d{1,2})?$")
156155
VERSION_RE = re.compile(r"^([23])\.(\d+)$")
157156

158157

159-
def parse_stdlib_versions_file() -> SupportedVersionsDict:
158+
class SupportedVersions:
159+
def __init__(self, module_versions: dict[str, tuple[VersionTuple, VersionTuple]]) -> None:
160+
self.module_versions = module_versions
161+
162+
def supported_versions_for_module(self, module_name: str) -> tuple[VersionTuple, VersionTuple]:
163+
while "." in module_name:
164+
if module_name in self.module_versions:
165+
return self.module_versions[module_name]
166+
module_name = ".".join(module_name.split(".")[:-1])
167+
return self.module_versions[module_name]
168+
169+
def is_supported(self, module_name: str, version: str) -> bool:
170+
version_tuple = tuple(map(int, version.split(".")))
171+
minimum, maximum = self.supported_versions_for_module(module_name)
172+
return minimum <= version_tuple <= maximum
173+
174+
175+
def parse_stdlib_versions_file() -> SupportedVersions:
160176
result: dict[str, tuple[VersionTuple, VersionTuple]] = {}
161177
with VERSIONS_PATH.open(encoding="UTF-8") as f:
162178
for line in f:
@@ -170,15 +186,7 @@ def parse_stdlib_versions_file() -> SupportedVersionsDict:
170186
min_version = _parse_version(m.group(2))
171187
max_version = _parse_version(m.group(3)) if m.group(3) else (99, 99)
172188
result[mod] = min_version, max_version
173-
return result
174-
175-
176-
def supported_versions_for_module(module_versions: SupportedVersionsDict, module_name: str) -> tuple[VersionTuple, VersionTuple]:
177-
while "." in module_name:
178-
if module_name in module_versions:
179-
return module_versions[module_name]
180-
module_name = ".".join(module_name.split(".")[:-1])
181-
return module_versions[module_name]
189+
return SupportedVersions(result)
182190

183191

184192
def _parse_version(v_str: str) -> tuple[int, int]:

tests/check_typeshed_structure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def check_no_symlinks() -> None:
130130

131131
def check_versions_file() -> None:
132132
"""Check that the stdlib/VERSIONS file has the correct format."""
133-
version_map = parse_stdlib_versions_file()
133+
version_map = parse_stdlib_versions_file().module_versions
134134
versions = list(version_map.keys())
135135

136136
sorted_versions = sorted(versions)

tests/mypy_test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
print_error,
3434
print_success_msg,
3535
spec_matches_path,
36-
supported_versions_for_module,
3736
venv_python,
3837
)
3938

@@ -311,15 +310,13 @@ def test_stdlib(args: TestConfig) -> TestResult:
311310

312311

313312
def remove_modules_not_in_python_version(paths: list[Path], py_version: VersionString) -> list[Path]:
314-
py_version_tuple = tuple(map(int, py_version.split(".")))
315313
module_versions = parse_stdlib_versions_file()
316314
new_paths: list[Path] = []
317315
for path in paths:
318316
if path.parts[0] != "stdlib" or path.suffix != ".pyi":
319317
continue
320318
module_name = stdlib_module_name_from_path(path)
321-
min_version, max_version = supported_versions_for_module(module_versions, module_name)
322-
if min_version <= py_version_tuple <= max_version:
319+
if module_versions.is_supported(module_name, py_version):
323320
new_paths.append(path)
324321
return new_paths
325322

tests/ty_test.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pathlib import Path
1010

1111
from ts_utils.paths import STDLIB_PATH, STUBS_PATH, TS_BASE_PATH
12-
from ts_utils.utils import parse_stdlib_versions_file, supported_versions_for_module
12+
from ts_utils.utils import parse_stdlib_versions_file
1313

1414
SUPPORTED_VERSIONS = ("3.10", "3.11", "3.12", "3.13", "3.14", "3.15")
1515
SUPPORTED_PLATFORMS = ("linux", "darwin", "win32")
@@ -19,7 +19,6 @@
1919

2020
def stdlib_files(version: str) -> list[Path]:
2121
"""Return the stdlib stubs available in the requested Python version."""
22-
version_tuple = tuple(map(int, version.split(".")))
2322
module_versions = parse_stdlib_versions_file()
2423
files: list[Path] = []
2524

@@ -33,8 +32,7 @@ def stdlib_files(version: str) -> list[Path]:
3332
parts = list(relative.parts[:-1])
3433
if relative.name != "__init__.pyi":
3534
parts.append(relative.stem)
36-
minimum, maximum = supported_versions_for_module(module_versions, ".".join(parts))
37-
if minimum <= version_tuple <= maximum:
35+
if module_versions.is_supported(".".join(parts), version):
3836
files.append(path)
3937

4038
return files

0 commit comments

Comments
 (0)