Bundle path handling in ts_utils.paths (#12805)

This commit is contained in:
Sebastian Rittau
2024-10-17 08:16:10 +02:00
committed by GitHub
parent 36fb63ebc8
commit 2d0f6d8277
10 changed files with 81 additions and 80 deletions

View File

@@ -13,17 +13,13 @@ import sys
from pathlib import Path
from ts_utils.metadata import read_metadata
from ts_utils.paths import REQUIREMENTS_PATH, STDLIB_PATH, STUBS_PATH, TEST_CASES_DIR, TESTS_DIR, tests_path
from ts_utils.utils import (
REQS_FILE,
STDLIB_PATH,
TEST_CASES_DIR,
TESTS_DIR,
get_all_testcase_directories,
get_gitignore_spec,
parse_requirements,
parse_stdlib_versions_file,
spec_matches_path,
tests_path,
)
extension_descriptions = {".pyi": "stub", ".py": ".py"}
@@ -66,7 +62,7 @@ def check_stdlib() -> None:
def check_stubs() -> None:
"""Check that the stubs directory contains only the correct files."""
gitignore_spec = get_gitignore_spec()
for dist in Path("stubs").iterdir():
for dist in STUBS_PATH.iterdir():
if spec_matches_path(gitignore_spec, dist):
continue
assert dist.is_dir(), f"Only directories allowed in stubs, got {dist}"
@@ -97,8 +93,8 @@ def check_distutils() -> None:
def all_relative_paths_in_directory(path: Path) -> set[Path]:
return {pyi.relative_to(path) for pyi in path.rglob("*.pyi")}
all_setuptools_files = all_relative_paths_in_directory(Path("stubs", "setuptools", "setuptools", "_distutils"))
all_distutils_files = all_relative_paths_in_directory(Path("stubs", "setuptools", "distutils"))
all_setuptools_files = all_relative_paths_in_directory(STUBS_PATH / "setuptools" / "setuptools" / "_distutils")
all_distutils_files = all_relative_paths_in_directory(STUBS_PATH / "setuptools" / "distutils")
assert all_setuptools_files and all_distutils_files, "Looks like this test might be out of date!"
extra_files = all_setuptools_files - all_distutils_files
joined = "\n".join(f" * {f}" for f in extra_files)
@@ -164,10 +160,10 @@ def check_requirement_pins() -> None:
"""Check that type checkers and linters are pinned to an exact version."""
requirements = parse_requirements()
for package in linters:
assert package in requirements, f"type checker/linter '{package}' not found in {REQS_FILE}"
assert package in requirements, f"type checker/linter '{package}' not found in {REQUIREMENTS_PATH.name}"
spec = requirements[package].specifier
assert len(spec) == 1, f"type checker/linter '{package}' has complex specifier in {REQS_FILE}"
msg = f"type checker/linter '{package}' is not pinned to an exact version in {REQS_FILE}"
assert len(spec) == 1, f"type checker/linter '{package}' has complex specifier in {REQUIREMENTS_PATH.name}"
msg = f"type checker/linter '{package}' is not pinned to an exact version in {REQUIREMENTS_PATH.name}"
assert str(spec).startswith("=="), msg

View File

@@ -22,11 +22,10 @@ from typing_extensions import Annotated, TypeAlias
import tomli
from packaging.requirements import Requirement
from ts_utils.metadata import PackageDependencies, get_recursive_requirements, read_metadata
from ts_utils.metadata import PackageDependencies, get_recursive_requirements, metadata_path, read_metadata
from ts_utils.paths import STDLIB_PATH, STUBS_PATH, TESTS_DIR, TS_BASE_PATH, distribution_path
from ts_utils.utils import (
PYTHON_VERSION,
STDLIB_PATH,
TESTS_DIR,
colored,
get_gitignore_spec,
get_mypy_req,
@@ -47,7 +46,7 @@ except ImportError:
SUPPORTED_VERSIONS = ["3.13", "3.12", "3.11", "3.10", "3.9", "3.8"]
SUPPORTED_PLATFORMS = ("linux", "win32", "darwin")
DIRECTORIES_TO_TEST = [Path("stdlib"), Path("stubs")]
DIRECTORIES_TO_TEST = [STDLIB_PATH, STUBS_PATH]
VersionString: TypeAlias = Annotated[str, "Must be one of the entries in SUPPORTED_VERSIONS"]
Platform: TypeAlias = Annotated[str, "Must be one of the entries in SUPPORTED_PLATFORMS"]
@@ -170,7 +169,7 @@ class MypyDistConf(NamedTuple):
def add_configuration(configurations: list[MypyDistConf], distribution: str) -> None:
with Path("stubs", distribution, "METADATA.toml").open("rb") as f:
with metadata_path(distribution).open("rb") as f:
data = tomli.load(f)
# TODO: This could be added to ts_utils.metadata, but is currently unused
@@ -229,7 +228,7 @@ def run_mypy(
"--platform",
args.platform,
"--custom-typeshed-dir",
str(Path(__file__).parent.parent),
str(TS_BASE_PATH),
"--strict",
# Stub completion is checked by pyright (--allow-*-defs)
"--allow-untyped-defs",
@@ -283,7 +282,7 @@ def add_third_party_files(
return
seen_dists.add(distribution)
seen_dists.update(r.name for r in typeshed_reqs)
root = Path("stubs", distribution)
root = distribution_path(distribution)
for name in os.listdir(root):
if name.startswith("."):
continue
@@ -319,7 +318,7 @@ def test_third_party_distribution(
print_error("no files found")
sys.exit(1)
mypypath = os.pathsep.join(str(Path("stubs", dist)) for dist in seen_dists)
mypypath = os.pathsep.join(str(distribution_path(dist)) for dist in seen_dists)
if args.verbose:
print(colored(f"\nMYPYPATH={mypypath}", "blue"))
result = run_mypy(
@@ -516,16 +515,12 @@ def test_third_party_stubs(args: TestConfig, tempdir: Path) -> TestSummary:
distributions_to_check: dict[str, PackageDependencies] = {}
for distribution in sorted(os.listdir("stubs")):
distribution_path = Path("stubs", distribution)
dist_path = distribution_path(distribution)
if spec_matches_path(gitignore_spec, distribution_path):
if spec_matches_path(gitignore_spec, dist_path):
continue
if (
distribution_path in args.filter
or Path("stubs") in args.filter
or any(distribution_path in path.parents for path in args.filter)
):
if dist_path in args.filter or STUBS_PATH in args.filter or any(dist_path in path.parents for path in args.filter):
metadata = read_metadata(distribution)
if not metadata.requires_python.contains(PYTHON_VERSION):
msg = (

View File

@@ -22,9 +22,9 @@ from pathlib import Path
from typing_extensions import TypeAlias
from ts_utils.metadata import get_recursive_requirements, read_metadata
from ts_utils.paths import STDLIB_PATH, TEST_CASES_DIR, TS_BASE_PATH, distribution_path
from ts_utils.utils import (
PYTHON_VERSION,
TEST_CASES_DIR,
DistributionTests,
colored,
distribution_info,
@@ -134,14 +134,14 @@ def setup_testcase_dir(package: DistributionTests, tempdir: Path, verbosity: Ver
# that has only the required stubs copied over.
new_typeshed = tempdir / TYPESHED
new_typeshed.mkdir()
shutil.copytree(Path("stdlib"), new_typeshed / "stdlib")
shutil.copytree(STDLIB_PATH, new_typeshed / "stdlib")
requirements = get_recursive_requirements(package.name)
# mypy refuses to consider a directory a "valid typeshed directory"
# unless there's a stubs/mypy-extensions path inside it,
# so add that to the list of stubs to copy over to the new directory
typeshed_requirements = [r.name for r in requirements.typeshed_pkgs]
for requirement in {package.name, *typeshed_requirements, "mypy-extensions"}:
shutil.copytree(Path("stubs", requirement), new_typeshed / "stubs" / requirement)
shutil.copytree(distribution_path(requirement), new_typeshed / "stubs" / requirement)
if requirements.external_pkgs:
venv_location = str(tempdir / VENV_DIR)
@@ -190,7 +190,7 @@ def run_testcases(
if package.is_stdlib:
python_exe = sys.executable
custom_typeshed = Path(__file__).parent.parent
custom_typeshed = TS_BASE_PATH
flags.append("--no-site-packages")
else:
custom_typeshed = tempdir / TYPESHED

View File

@@ -10,7 +10,8 @@ import sys
from importlib.util import find_spec
from pathlib import Path
from ts_utils.utils import TEST_CASES_DIR, colored, test_cases_path
from ts_utils.paths import TEST_CASES_DIR, test_cases_path
from ts_utils.utils import colored
_STRICTER_CONFIG_FILE = "pyrightconfig.stricter.json"
_TESTCASES_CONFIG_FILE = "pyrightconfig.testcases.json"

View File

@@ -13,7 +13,8 @@ import subprocess
import sys
from pathlib import Path
from ts_utils.utils import allowlist_stubtest_arguments, allowlists_path
from ts_utils.paths import TS_BASE_PATH, allowlists_path
from ts_utils.utils import allowlist_stubtest_arguments
def run_stubtest(typeshed_dir: Path) -> int:
@@ -57,4 +58,4 @@ def run_stubtest(typeshed_dir: Path) -> int:
if __name__ == "__main__":
sys.exit(run_stubtest(typeshed_dir=Path(".")))
sys.exit(run_stubtest(typeshed_dir=TS_BASE_PATH))

View File

@@ -14,17 +14,16 @@ from textwrap import dedent
from typing import NoReturn
from ts_utils.metadata import NoSuchStubError, get_recursive_requirements, read_metadata
from ts_utils.paths import STUBS_PATH, allowlists_path, tests_path
from ts_utils.utils import (
PYTHON_VERSION,
allowlist_stubtest_arguments,
allowlists_path,
colored,
get_mypy_req,
print_divider,
print_error,
print_info,
print_success_msg,
tests_path,
)
@@ -386,11 +385,10 @@ def main() -> NoReturn:
parser.add_argument("dists", metavar="DISTRIBUTION", type=str, nargs=argparse.ZERO_OR_MORE)
args = parser.parse_args()
typeshed_dir = Path(".").resolve()
if len(args.dists) == 0:
dists = sorted((typeshed_dir / "stubs").iterdir())
dists = sorted(STUBS_PATH.iterdir())
else:
dists = [typeshed_dir / "stubs" / d for d in args.dists]
dists = [STUBS_PATH / d for d in args.dists]
result = 0
for i, dist in enumerate(dists):