Repository navigation
Expand file tree
/
Copy pathpyrefly_test.py
More file actions
executable file
·80 lines (63 loc) · 3.16 KB
/
Copy pathpyrefly_test.py
File metadata and controls
executable file
·80 lines (63 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
"""Run pyrefly on typeshed's stdlib and third-party stubs."""
from __future__ import annotations
import argparse
import subprocess
from pathlib import Path
from ts_utils.paths import STDLIB_PATH, STUBS_PATH, TS_BASE_PATH
from ts_utils.stubs import path_stubs, stdlib_stubs, third_party_stubs
SUPPORTED_VERSIONS = ("3.10", "3.11", "3.12", "3.13", "3.14", "3.15")
SUPPORTED_PLATFORMS = ("linux", "darwin", "win32")
# requests is obsolete and the typed runtime package is installed instead.
EXCLUDED_STUBS = {"requests"}
def stdlib_files(version: str) -> list[Path]:
"""Return the stdlib stubs available in the requested Python version."""
# pyrefly cannot resolve relative imports in the legacy distutils stubs.
return [stub.path for stub in stdlib_stubs(version) if stub.module_parts[0] != "distutils"]
def third_party_files() -> list[Path]:
return [stub.path for stub in third_party_stubs() if stub.upstream_distribution not in EXCLUDED_STUBS]
def _filter_files(files: list[Path], paths: list[Path], root: Path) -> list[Path]:
selected_paths = [path if path.is_absolute() else TS_BASE_PATH / path for path in paths]
selected_files = {file for path in selected_paths if path.is_relative_to(root) for file in path_stubs(path)}
return [file for file in files if file in selected_files]
def main() -> int:
parser = argparse.ArgumentParser(description="Typecheck typeshed's stdlib and third-party stubs with pyrefly.")
parser.add_argument("paths", nargs="*", type=Path, help="Specific stdlib or third-party stubs to check")
parser.add_argument("--python", type=Path, help="Python interpreter used to resolve third-party imports")
parser.add_argument("--python-version", choices=SUPPORTED_VERSIONS, default=SUPPORTED_VERSIONS[0])
parser.add_argument("--platform", choices=SUPPORTED_PLATFORMS, default="linux")
args = parser.parse_args()
stdlib = stdlib_files(args.python_version)
third_party = third_party_files()
if args.paths:
stdlib = _filter_files(stdlib, args.paths, STDLIB_PATH)
third_party = _filter_files(third_party, args.paths, STUBS_PATH)
files = [*stdlib, *third_party]
if not files:
print("No stubs to check with pyrefly.", flush=True)
return 0
command = [
"pyrefly",
"check",
"--config",
str(TS_BASE_PATH / "pyrefly.toml"),
"--typeshed-path",
str(TS_BASE_PATH),
"--python-version",
args.python_version,
"--python-platform",
args.platform,
"--output-format",
"min-text",
]
if args.python is not None:
command.extend(("--python-interpreter-path", str(args.python)))
for path in sorted(STUBS_PATH.iterdir()):
# requests is obsolete and the typed runtime package provides requests._types.
if path.is_dir() and path.name != "requests":
command.extend(("--search-path", str(path)))
command.extend(map(str, files))
print(f"Checking {len(files)} stubs with pyrefly ({args.python_version}, {args.platform})...", flush=True)
return subprocess.run(command, check=False).returncode
if __name__ == "__main__":
raise SystemExit(main())