From 6b3de1517ad5433e58cb4edc0eba2dee0a92bf58 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 5 Jan 2026 14:55:08 +0000 Subject: [PATCH] [ty] Improve tracebacks when installing dependencies fails in ty_benchmark (#22399) --- scripts/ty_benchmark/src/benchmark/projects.py | 2 +- scripts/ty_benchmark/src/benchmark/run.py | 4 +++- .../src/benchmark/test_lsp_diagnostics.py | 4 +++- scripts/ty_benchmark/src/benchmark/venv.py | 18 +++++++++++------- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/scripts/ty_benchmark/src/benchmark/projects.py b/scripts/ty_benchmark/src/benchmark/projects.py index a2c3278969..b4635569fa 100644 --- a/scripts/ty_benchmark/src/benchmark/projects.py +++ b/scripts/ty_benchmark/src/benchmark/projects.py @@ -92,7 +92,7 @@ class Project(NamedTuple): ) except subprocess.CalledProcessError as e: - raise RuntimeError(f"Failed to clone {self.name}: {e.stderr}") + raise RuntimeError(f"Failed to clone {self.name}:\n\n{e.stderr}") from e logging.info(f"Cloned {self.name} to {checkout_dir}.") diff --git a/scripts/ty_benchmark/src/benchmark/run.py b/scripts/ty_benchmark/src/benchmark/run.py index 0c0cb34791..07ce4b2756 100644 --- a/scripts/ty_benchmark/src/benchmark/run.py +++ b/scripts/ty_benchmark/src/benchmark/run.py @@ -147,7 +147,9 @@ def main() -> None: cwd = Path(tempdir) project.clone(cwd) - venv = Venv.create(cwd, project.python_version) + venv = Venv.create( + project=project.name, parent=cwd, python_version=project.python_version + ) venv.install(project.install_arguments) commands = [] diff --git a/scripts/ty_benchmark/src/benchmark/test_lsp_diagnostics.py b/scripts/ty_benchmark/src/benchmark/test_lsp_diagnostics.py index 3a66a15e58..7ce2265079 100644 --- a/scripts/ty_benchmark/src/benchmark/test_lsp_diagnostics.py +++ b/scripts/ty_benchmark/src/benchmark/test_lsp_diagnostics.py @@ -42,7 +42,9 @@ def project_setup( cwd = Path(tempdir) project.clone(cwd) - venv = Venv.create(cwd, project.python_version) + venv = Venv.create( + project=project.name, parent=cwd, python_version=project.python_version + ) venv.install(project.install_arguments) yield project, venv diff --git a/scripts/ty_benchmark/src/benchmark/venv.py b/scripts/ty_benchmark/src/benchmark/venv.py index 1462780545..b80de002f7 100644 --- a/scripts/ty_benchmark/src/benchmark/venv.py +++ b/scripts/ty_benchmark/src/benchmark/venv.py @@ -3,15 +3,15 @@ from __future__ import annotations import logging import subprocess import sys +from dataclasses import dataclass from pathlib import Path +@dataclass(frozen=True, kw_only=True, slots=True) class Venv: + project_name: str project_path: Path - def __init__(self, path: Path): - self.project_path = path - @property def path(self) -> Path: return self.project_path / "venv" @@ -36,7 +36,7 @@ class Venv: return self.bin / f"{name}{extension}" @staticmethod - def create(parent: Path, python_version: str) -> Venv: + def create(*, project: str, parent: Path, python_version: str) -> Venv: """Creates a new, empty virtual environment.""" command = [ @@ -53,9 +53,10 @@ class Venv: command, cwd=parent, check=True, capture_output=True, text=True ) except subprocess.CalledProcessError as e: - raise RuntimeError(f"Failed to create venv: {e.stderr}") + msg = f"Failed to create venv for {project}:\n\n{e.stderr}" + raise RuntimeError(msg) from e - return Venv(parent) + return Venv(project_name=project, project_path=parent) def install(self, pip_install_args: list[str]) -> None: """Installs the dependencies required to type check the project.""" @@ -87,4 +88,7 @@ class Venv: text=True, ) except subprocess.CalledProcessError as e: - raise RuntimeError(f"Failed to install dependencies: {e.stderr}") + msg = ( + f"Failed to install dependencies for {self.project_name}:\n\n{e.stderr}" + ) + raise RuntimeError(msg) from e