Skip to content

Commit cc40120

Browse files
authored
mypy_test: Exclude sub-modules not in current Py version (#12352)
1 parent 274b10d commit cc40120

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

tests/_utils.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from collections.abc import Iterable, Mapping
88
from functools import lru_cache
99
from pathlib import Path
10-
from typing import Any, Final, NamedTuple, Tuple
10+
from typing import Any, Dict, Final, NamedTuple, Tuple
1111
from typing_extensions import TypeAlias
1212

1313
import pathspec
@@ -108,14 +108,14 @@ def get_mypy_req() -> str:
108108
# ====================================================================
109109

110110
VersionTuple: TypeAlias = Tuple[int, int]
111-
111+
SupportedVersionsDict: TypeAlias = Dict[str, Tuple[VersionTuple, VersionTuple]]
112112

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

117117

118-
def parse_stdlib_versions_file() -> dict[str, tuple[VersionTuple, VersionTuple]]:
118+
def parse_stdlib_versions_file() -> SupportedVersionsDict:
119119
result: dict[str, tuple[VersionTuple, VersionTuple]] = {}
120120
with VERSIONS_PATH.open(encoding="UTF-8") as f:
121121
for line in f:

tests/mypy_test.py

+40-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
from _metadata import PackageDependencies, get_recursive_requirements, read_metadata
2525
from _utils import (
2626
PYTHON_VERSION,
27+
STDLIB_PATH,
2728
TESTS_DIR,
29+
SupportedVersionsDict,
30+
VersionTuple,
2831
colored,
2932
get_gitignore_spec,
3033
get_mypy_req,
@@ -332,15 +335,12 @@ def test_third_party_distribution(
332335

333336
def test_stdlib(args: TestConfig) -> TestResult:
334337
files: list[Path] = []
335-
stdlib = Path("stdlib")
336-
supported_versions = parse_stdlib_versions_file()
337-
for name in os.listdir(stdlib):
338-
if name in ("VERSIONS", TESTS_DIR) or name.startswith("."):
338+
for file in STDLIB_PATH.iterdir():
339+
if file.name in ("VERSIONS", TESTS_DIR) or file.name.startswith("."):
339340
continue
340-
module = Path(name).stem
341-
module_min_version, module_max_version = supported_versions[module]
342-
if module_min_version <= tuple(map(int, args.version.split("."))) <= module_max_version:
343-
add_files(files, (stdlib / name), args)
341+
add_files(files, file, args)
342+
343+
files = remove_modules_not_in_python_version(files, args.version)
344344

345345
if not files:
346346
return TestResult(MypyResult.SUCCESS, 0)
@@ -351,6 +351,38 @@ def test_stdlib(args: TestConfig) -> TestResult:
351351
return TestResult(result, len(files))
352352

353353

354+
def remove_modules_not_in_python_version(paths: list[Path], py_version: VersionString) -> list[Path]:
355+
py_version_tuple = tuple(map(int, py_version.split(".")))
356+
module_versions = parse_stdlib_versions_file()
357+
new_paths: list[Path] = []
358+
for path in paths:
359+
if path.parts[0] != "stdlib" or path.suffix != ".pyi":
360+
continue
361+
module_name = stdlib_module_name_from_path(path)
362+
min_version, max_version = supported_versions_for_module(module_versions, module_name)
363+
if min_version <= py_version_tuple <= max_version:
364+
new_paths.append(path)
365+
return new_paths
366+
367+
368+
def stdlib_module_name_from_path(path: Path) -> str:
369+
assert path.parts[0] == "stdlib"
370+
assert path.suffix == ".pyi"
371+
parts = list(path.parts[1:-1])
372+
if path.parts[-1] != "__init__.pyi":
373+
# TODO: Python 3.9+: Use removesuffix.
374+
parts.append(path.parts[-1][:-4])
375+
return ".".join(parts)
376+
377+
378+
def supported_versions_for_module(module_versions: SupportedVersionsDict, module_name: str) -> tuple[VersionTuple, VersionTuple]:
379+
while "." in module_name:
380+
if module_name in module_versions:
381+
return module_versions[module_name]
382+
module_name = ".".join(module_name.split(".")[:-1])
383+
return module_versions[module_name]
384+
385+
354386
@dataclass
355387
class TestSummary:
356388
mypy_result: MypyResult = MypyResult.SUCCESS

0 commit comments

Comments
 (0)