From 43084249ee830a0677fc973b64ecda028a1fa893 Mon Sep 17 00:00:00 2001 From: Jo <10510431+j178@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:14:05 +0800 Subject: [PATCH] Add mypy type check for uv-python scripts (#5332) ## Summary Per https://github.com/astral-sh/uv/pull/4853#issuecomment-2212505407 > If we're going to aim for full type coverage, we should probably follow this by adding type checking in CI too otherwise it seems too easy for it to become out of date. --- .github/workflows/ci.yml | 17 ++++++++++++-- crates/uv-python/fetch-download-metadata.py | 22 +++++++++---------- .../uv-python/template-download-metadata.py | 9 ++++---- pyproject.toml | 4 ++++ 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 74f86f34a..d9987cf20 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,14 +59,27 @@ jobs: - name: "Prettier" run: npx prettier --check "**/*.{json5,yaml,yml}" + - name: "README check" + run: python scripts/transform_readme.py --target pypi + + python-lint: + name: "Python lint" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: 3.12 + - name: "Ruff format" run: pipx run ruff format --diff . - name: "Ruff check" run: pipx run ruff check . - - name: "README check" - run: python scripts/transform_readme.py --target pypi + - name: "Mypy check" + run: pipx run --python 3.12 mypy cargo-clippy: needs: determine_changes diff --git a/crates/uv-python/fetch-download-metadata.py b/crates/uv-python/fetch-download-metadata.py index 55e3894fa..6ab3b9e2e 100755 --- a/crates/uv-python/fetch-download-metadata.py +++ b/crates/uv-python/fetch-download-metadata.py @@ -78,7 +78,7 @@ class ImplementationName(StrEnum): class PythonDownload: version: Version triple: PlatformTriple - flavor: str | None + flavor: str implementation: ImplementationName filename: str url: str @@ -185,7 +185,7 @@ class CPythonFinder(Finder): # Collapse CPython variants to a single URL flavor per triple downloads = [] for choices in results.values(): - flavors = {} + flavors: dict[PlatformTriple, tuple[PythonDownload, int]] = {} for choice in choices: priority = self._get_flavor_priority(choice.flavor) existing = flavors.get(choice.triple) @@ -250,12 +250,12 @@ class CPythonFinder(Finder): # Ex) # https://github.com/indygreg/python-build-standalone/releases/download/20240107/cpython-3.12.1%2B20240107-aarch64-unknown-linux-gnu-lto-full.tar.zst if url.endswith(".sha256"): - return + return None filename = unquote(url.rsplit("/", maxsplit=1)[-1]) match = self._filename_re.match(filename) if match is None: - return + return None version, triple = match.groups() if triple.endswith("-full"): @@ -265,14 +265,14 @@ class CPythonFinder(Finder): if match is not None: triple, flavor = match.groups() else: - flavor = None + flavor = "" if flavor in self.HIDDEN_FLAVORS: - return + return None version = Version.from_str(version) triple = self._normalize_triple(triple) if triple is None: - return + return None return PythonDownload( version=version, @@ -286,7 +286,7 @@ class CPythonFinder(Finder): def _normalize_triple(self, triple: str) -> PlatformTriple | None: if "-static" in triple: logging.debug("Skipping %r: static unsupported", triple) - return + return None triple = self.SPECIAL_TRIPLES.get(triple, triple) pieces = triple.split("-") @@ -300,7 +300,7 @@ class CPythonFinder(Finder): libc = "none" except IndexError: logging.debug("Skipping %r: unknown triple", triple) - return + return None return PlatformTriple(arch, operating_system, libc) @@ -313,7 +313,7 @@ class CPythonFinder(Finder): def _normalize_os(self, os: str) -> str: return os - def _get_flavor_priority(self, flavor: str | None) -> int: + def _get_flavor_priority(self, flavor: str) -> int: """Returns the priority of a flavor. Lower is better.""" try: pref = self.FLAVOR_PREFERENCES.index(flavor) @@ -385,7 +385,7 @@ async def find() -> None: render(downloads) -def main(): +def main() -> None: parser = argparse.ArgumentParser(description="Fetch Python version metadata.") parser.add_argument( "-v", diff --git a/crates/uv-python/template-download-metadata.py b/crates/uv-python/template-download-metadata.py index 50afaefb6..f91bbbb2c 100755 --- a/crates/uv-python/template-download-metadata.py +++ b/crates/uv-python/template-download-metadata.py @@ -14,12 +14,13 @@ Usage: uv run --isolated -- crates/uv-python/template-download-metadata.py """ -import sys -import logging import argparse import json +import logging import subprocess +import sys from pathlib import Path +from typing import Any import chevron_blue @@ -66,10 +67,10 @@ def prepare_value(value: dict) -> dict: return value -def main(): +def main() -> None: debug = logging.getLogger().getEffectiveLevel() <= logging.DEBUG - data = {} + data: dict[str, Any] = {} data["generated_with"] = Path(__file__).relative_to(WORKSPACE_ROOT).as_posix() data["generated_from"] = TEMPLATE.relative_to(WORKSPACE_ROOT).as_posix() data["versions"] = [ diff --git a/pyproject.toml b/pyproject.toml index 72477c97a..4b5247c95 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -72,3 +72,7 @@ version_files = [ [tool.uv] managed = false + +[tool.mypy] +ignore_missing_imports = true +files = "crates/uv-python/*.py"