From 12117ad0c6255c24335f1c4202d8134334138149 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Thu, 7 May 2026 09:03:37 -0700 Subject: [PATCH] fix: narrow exception types in get_models, fix patch targets in tests, add type annotation --- app/eval/cforch.py | 4 +++- app/eval/embed_bench.py | 6 ++++-- tests/test_embed_bench.py | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/eval/cforch.py b/app/eval/cforch.py index f498170..93a6cbf 100644 --- a/app/eval/cforch.py +++ b/app/eval/cforch.py @@ -12,6 +12,8 @@ Route prefixes when mounted at /api in api.py: """ from __future__ import annotations +from pathlib import Path + from fastapi import APIRouter from app.cforch import router as _cforch_router @@ -28,7 +30,7 @@ router.include_router(_plans_router, prefix="/plans-bench") router.include_router(_embed_router, prefix="/embed-bench") -def set_config_dir(path) -> None: +def set_config_dir(path: Path | None) -> None: """Propagate config dir override to all sub-modules -- used by tests.""" import app.cforch as _cforch_mod import app.style as _style_mod diff --git a/app/eval/embed_bench.py b/app/eval/embed_bench.py index e62efce..a46e221 100644 --- a/app/eval/embed_bench.py +++ b/app/eval/embed_bench.py @@ -100,6 +100,8 @@ def get_models() -> dict: "name": entry.get("name", ""), "size": entry.get("size", 0), }) - except Exception as exc: - logger.warning("Failed to list Ollama models: %s", exc) + except httpx.HTTPStatusError as exc: + logger.warning("Ollama /api/tags returned HTTP %s: %s", exc.response.status_code, exc) + except httpx.RequestError as exc: + logger.warning("Failed to reach Ollama for model list: %s", exc) return {"models": models, "ollama_url": ollama} diff --git a/tests/test_embed_bench.py b/tests/test_embed_bench.py index 180517c..4488e06 100644 --- a/tests/test_embed_bench.py +++ b/tests/test_embed_bench.py @@ -73,7 +73,7 @@ def test_models_returns_list_with_mock(client, tmp_path): } mock_resp.raise_for_status = MagicMock() - with patch("httpx.get", return_value=mock_resp): + with patch("app.eval.embed_bench.httpx.get", return_value=mock_resp): r = client.get("/api/embed-bench/models") assert r.status_code == 200 @@ -85,7 +85,7 @@ def test_models_returns_list_with_mock(client, tmp_path): def test_models_returns_empty_on_ollama_error(client, tmp_path): """GET /api/embed-bench/models returns empty list if Ollama unreachable.""" import httpx - with patch("httpx.get", side_effect=httpx.ConnectError("refused")): + with patch("app.eval.embed_bench.httpx.get", side_effect=httpx.ConnectError("refused")): r = client.get("/api/embed-bench/models") assert r.status_code == 200 assert r.json()["models"] == []