mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-08 04:54:47 +08:00
Bundle path handling in ts_utils.paths (#12805)
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
import urllib.parse
|
||||
from collections.abc import Mapping
|
||||
@@ -18,6 +17,7 @@ import tomli
|
||||
from packaging.requirements import Requirement
|
||||
from packaging.specifiers import Specifier
|
||||
|
||||
from .paths import PYPROJECT_PATH, STUBS_PATH, distribution_path
|
||||
from .utils import cache
|
||||
|
||||
__all__ = [
|
||||
@@ -43,20 +43,15 @@ def _is_list_of_strings(obj: object) -> TypeGuard[list[str]]:
|
||||
|
||||
@cache
|
||||
def _get_oldest_supported_python() -> str:
|
||||
with open("pyproject.toml", "rb") as config:
|
||||
with PYPROJECT_PATH.open("rb") as config:
|
||||
val = tomli.load(config)["tool"]["typeshed"]["oldest_supported_python"]
|
||||
assert type(val) is str
|
||||
return val
|
||||
|
||||
|
||||
def stubs_path(distribution: str) -> Path:
|
||||
"""Return the path to the directory of a third-party distribution."""
|
||||
return Path("stubs", distribution)
|
||||
|
||||
|
||||
def metadata_path(distribution: str) -> Path:
|
||||
"""Return the path to the METADATA.toml file of a third-party distribution."""
|
||||
return stubs_path(distribution) / "METADATA.toml"
|
||||
return distribution_path(distribution) / "METADATA.toml"
|
||||
|
||||
|
||||
@final
|
||||
@@ -317,7 +312,7 @@ class PackageDependencies(NamedTuple):
|
||||
|
||||
@cache
|
||||
def get_pypi_name_to_typeshed_name_mapping() -> Mapping[str, str]:
|
||||
return {read_metadata(typeshed_name).stub_distribution: typeshed_name for typeshed_name in os.listdir("stubs")}
|
||||
return {read_metadata(dir.name).stub_distribution: dir.name for dir in STUBS_PATH.iterdir()}
|
||||
|
||||
|
||||
@cache
|
||||
|
||||
39
lib/ts_utils/paths.py
Normal file
39
lib/ts_utils/paths.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from pathlib import Path
|
||||
from typing import Final
|
||||
|
||||
# TODO: Use base path relative to this file. Currently, ts_utils gets
|
||||
# installed into the user's virtual env, so we can't determine the path
|
||||
# to typeshed. Installing ts_utils editable would solve that, see
|
||||
# https://github.com/python/typeshed/pull/12806.
|
||||
TS_BASE_PATH: Final = Path("")
|
||||
STDLIB_PATH: Final = TS_BASE_PATH / "stdlib"
|
||||
STUBS_PATH: Final = TS_BASE_PATH / "stubs"
|
||||
|
||||
PYPROJECT_PATH: Final = TS_BASE_PATH / "pyproject.toml"
|
||||
REQUIREMENTS_PATH: Final = TS_BASE_PATH / "requirements-tests.txt"
|
||||
|
||||
TESTS_DIR: Final = "@tests"
|
||||
TEST_CASES_DIR: Final = "test_cases"
|
||||
|
||||
|
||||
def distribution_path(distribution_name: str) -> Path:
|
||||
"""Return the path to the directory of a third-party distribution."""
|
||||
return STUBS_PATH / distribution_name
|
||||
|
||||
|
||||
def tests_path(distribution_name: str) -> Path:
|
||||
if distribution_name == "stdlib":
|
||||
return STDLIB_PATH / TESTS_DIR
|
||||
else:
|
||||
return STUBS_PATH / distribution_name / TESTS_DIR
|
||||
|
||||
|
||||
def test_cases_path(distribution_name: str) -> Path:
|
||||
return tests_path(distribution_name) / TEST_CASES_DIR
|
||||
|
||||
|
||||
def allowlists_path(distribution_name: str) -> Path:
|
||||
if distribution_name == "stdlib":
|
||||
return tests_path("stdlib") / "stubtest_allowlists"
|
||||
else:
|
||||
return tests_path(distribution_name)
|
||||
@@ -21,10 +21,9 @@ except ImportError:
|
||||
return text
|
||||
|
||||
|
||||
PYTHON_VERSION: Final = f"{sys.version_info.major}.{sys.version_info.minor}"
|
||||
from .paths import REQUIREMENTS_PATH, STDLIB_PATH, STUBS_PATH, TEST_CASES_DIR, allowlists_path, test_cases_path
|
||||
|
||||
STDLIB_PATH = Path("stdlib")
|
||||
STUBS_PATH = Path("stubs")
|
||||
PYTHON_VERSION: Final = f"{sys.version_info.major}.{sys.version_info.minor}"
|
||||
|
||||
|
||||
# A backport of functools.cache for Python <3.9
|
||||
@@ -90,16 +89,14 @@ def venv_python(venv_dir: Path) -> Path:
|
||||
# ====================================================================
|
||||
|
||||
|
||||
REQS_FILE: Final = "requirements-tests.txt"
|
||||
|
||||
|
||||
@cache
|
||||
def parse_requirements() -> Mapping[str, Requirement]:
|
||||
"""Return a dictionary of requirements from the requirements file."""
|
||||
|
||||
with open(REQS_FILE, encoding="UTF-8") as requirements_file:
|
||||
with REQUIREMENTS_PATH.open(encoding="UTF-8") as requirements_file:
|
||||
stripped_lines = map(strip_comments, requirements_file)
|
||||
requirements = map(Requirement, filter(None, stripped_lines))
|
||||
stripped_more = [li for li in stripped_lines if not li.startswith("-")]
|
||||
requirements = map(Requirement, filter(None, stripped_more))
|
||||
return {requirement.name: requirement for requirement in requirements}
|
||||
|
||||
|
||||
@@ -155,10 +152,6 @@ def _parse_version(v_str: str) -> tuple[int, int]:
|
||||
# ====================================================================
|
||||
|
||||
|
||||
TESTS_DIR: Final = "@tests"
|
||||
TEST_CASES_DIR: Final = "test_cases"
|
||||
|
||||
|
||||
class DistributionTests(NamedTuple):
|
||||
name: str
|
||||
test_cases_path: Path
|
||||
@@ -179,17 +172,6 @@ def distribution_info(distribution_name: str) -> DistributionTests:
|
||||
raise RuntimeError(f"No test cases found for {distribution_name!r}!")
|
||||
|
||||
|
||||
def tests_path(distribution_name: str) -> Path:
|
||||
if distribution_name == "stdlib":
|
||||
return STDLIB_PATH / TESTS_DIR
|
||||
else:
|
||||
return STUBS_PATH / distribution_name / TESTS_DIR
|
||||
|
||||
|
||||
def test_cases_path(distribution_name: str) -> Path:
|
||||
return tests_path(distribution_name) / TEST_CASES_DIR
|
||||
|
||||
|
||||
def get_all_testcase_directories() -> list[DistributionTests]:
|
||||
testcase_directories: list[DistributionTests] = []
|
||||
for distribution_path in STUBS_PATH.iterdir():
|
||||
@@ -201,13 +183,6 @@ def get_all_testcase_directories() -> list[DistributionTests]:
|
||||
return [distribution_info("stdlib"), *sorted(testcase_directories)]
|
||||
|
||||
|
||||
def allowlists_path(distribution_name: str) -> Path:
|
||||
if distribution_name == "stdlib":
|
||||
return tests_path("stdlib") / "stubtest_allowlists"
|
||||
else:
|
||||
return tests_path(distribution_name)
|
||||
|
||||
|
||||
def allowlists(distribution_name: str) -> list[str]:
|
||||
prefix = "" if distribution_name == "stdlib" else "stubtest_allowlist_"
|
||||
version_id = f"py{sys.version_info.major}{sys.version_info.minor}"
|
||||
|
||||
Reference in New Issue
Block a user