Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Isolate DSPy cache in tests (#9795)

Isaac Millerefb7fee7463d
diff --git a/tests/conftest.py b/tests/conftest.py
index 81680d970..aabbe43fa 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,5 +1,8 @@
import copy
import os
+from collections.abc import Iterator
+from pathlib import Path
+from typing import Any
import pytest
@@ -8,16 +11,31 @@ from tests.test_utils.server import litellm_test_server, read_litellm_test_serve
SKIP_DEFAULT_FLAGS = ["reliability", "extra", "llm_call", "deno"]
[email protected](autouse=True)
-def clear_settings():
- """Ensures that the settings are cleared after each test."""
+def _close_cache(cache: Any) -> None:
+ disk_cache = getattr(cache, "disk_cache", None)
+ if hasattr(disk_cache, "close"):
+ disk_cache.close()
- yield
[email protected](autouse=True)
+def clear_settings(tmp_path: Path) -> Iterator[None]:
+ """Ensure each test gets fresh DSPy settings and an isolated cache."""
import dspy
- from dspy.dsp.utils.settings import DEFAULT_CONFIG
- dspy.configure(**copy.deepcopy(DEFAULT_CONFIG), inherit_config=False)
+ original_cache = dspy.cache
+ dspy.configure_cache(disk_cache_dir=tmp_path / ".dspy_cache")
+ try:
+ yield
+ finally:
+ from dspy.dsp.utils.settings import DEFAULT_CONFIG
+
+ try:
+ dspy.configure(**copy.deepcopy(DEFAULT_CONFIG), inherit_config=False)
+ finally:
+ try:
+ _close_cache(dspy.cache)
+ finally:
+ dspy.cache = original_cache
@pytest.fixture