Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Style fixing for tests/ (part 3) (#8227)

Chen Qiana15667410547
diff --git a/tests/retrievers/test_embeddings.py b/tests/retrievers/test_embeddings.py
index 2d1781db7..588dfa3a5 100644
--- a/tests/retrievers/test_embeddings.py
+++ b/tests/retrievers/test_embeddings.py
@@ -1,6 +1,7 @@
-from concurrent.futures import ThreadPoolExecutor, as_completed
+from concurrent.futures import ThreadPoolExecutor
+
import numpy as np
-import pytest
+
from dspy.retrievers.embeddings import Embeddings
diff --git a/tests/signatures/test_adapter_image.py b/tests/signatures/test_adapter_image.py
index c2d026fd5..4fdc99c6e 100644
--- a/tests/signatures/test_adapter_image.py
+++ b/tests/signatures/test_adapter_image.py
@@ -1,48 +1,47 @@
-from typing import Dict, List, Optional, Tuple
+import os
+import tempfile
+from io import BytesIO
+from typing import List, Optional
+import pydantic
import pytest
-from PIL import Image as PILImage
import requests
-from io import BytesIO
+from PIL import Image as PILImage
import dspy
-from dspy import Predict
-from dspy.utils.dummies import DummyLM
from dspy.adapters.types.image import encode_image
-import tempfile
-import pydantic
-import os
+from dspy.utils.dummies import DummyLM
@pytest.fixture
def sample_pil_image():
"""Fixture to provide a sample image for testing"""
- url = 'https://images.dog.ceo/breeds/dane-great/n02109047_8912.jpg'
+ url = "https://images.dog.ceo/breeds/dane-great/n02109047_8912.jpg"
response = requests.get(url)
response.raise_for_status()
return PILImage.open(BytesIO(response.content))
+
@pytest.fixture
def sample_dspy_image_download():
return dspy.Image.from_url("https://images.dog.ceo/breeds/dane-great/n02109047_8912.jpg", download=True)
+
@pytest.fixture
def sample_url():
return "https://images.dog.ceo/breeds/dane-great/n02109047_8912.jpg"
+
@pytest.fixture
def sample_dspy_image_no_download():
return dspy.Image.from_url("https://images.dog.ceo/breeds/dane-great/n02109047_8912.jpg", download=False)
+
def count_messages_with_image_url_pattern(messages):
- pattern = {
- 'type': 'image_url',
- 'image_url': {
- 'url': lambda x: isinstance(x, str)
- }
- }
-
+ pattern = {"type": "image_url", "image_url": {"url": lambda x: isinstance(x, str)}}
+
try:
+
def check_pattern(obj, pattern):
if isinstance(pattern, dict):
if not isinstance(obj, dict):
@@ -51,7 +50,7 @@ def count_messages_with_image_url_pattern(messages):
if callable(pattern):
return pattern(obj)
return obj == pattern
-
+
def count_patterns(obj, pattern):
count = 0
if check_pattern(obj, pattern):
@@ -61,71 +60,84 @@ def count_messages_with_image_url_pattern(messages):
if isinstance(obj, (list, tuple)):
count += sum(count_patterns(v, pattern) for v in obj)
return count
-
+
return count_patterns(messages, pattern)
except Exception:
return 0
+
def setup_predictor(signature, expected_output):
"""Helper to set up a predictor with DummyLM"""
lm = DummyLM([expected_output])
dspy.settings.configure(lm=lm)
return dspy.Predict(signature), lm
[email protected]("test_case", [
- {
- "name": "probabilistic_classification",
- "signature": "image: dspy.Image, class_labels: List[str] -> probabilities: Dict[str, float]",
- "inputs": {"image": "https://example.com/dog.jpg", "class_labels": ["dog", "cat", "bird"]},
- "key_output": "probabilities",
- "expected": {"probabilities": {"dog": 0.8, "cat": 0.1, "bird": 0.1}}
- },
- {
- "name": "image_to_code",
- "signature": "ui_image: dspy.Image, target_language: str -> generated_code: str",
- "inputs": {"ui_image": "https://example.com/button.png", "target_language": "HTML"},
- "key_output": "generated_code",
- "expected": {"generated_code": "<button>Click me</button>"}
- },
- {
- "name": "bbox_detection",
- "signature": "image: dspy.Image -> bboxes: List[Tuple[int, int, int, int]]",
- "inputs": {"image": "https://example.com/image.jpg"},
- "key_output": "bboxes",
- "expected": {"bboxes": [(10, 20, 30, 40), (50, 60, 70, 80)]}
- },
- {
- "name": "multilingual_caption",
- "signature": "image: dspy.Image, languages: List[str] -> captions: Dict[str, str]",
- "inputs": {"image": "https://example.com/dog.jpg", "languages": ["en", "es", "fr"]},
- "key_output": "captions",
- "expected": {"captions": {"en": "A golden retriever", "es": "Un golden retriever", "fr": "Un golden retriever"}}
- }
-])
+
+ "test_case",
+ [
+ {
+ "name": "probabilistic_classification",
+ "signature": "image: dspy.Image, class_labels: List[str] -> probabilities: Dict[str, float]",
+ "inputs": {"image": "https://example.com/dog.jpg", "class_labels": ["dog", "cat", "bird"]},
+ "key_output": "probabilities",
+ "expected": {"probabilities": {"dog": 0.8, "cat": 0.1, "bird": 0.1}},
+ },
+ {
+ "name": "image_to_code",
+ "signature": "ui_image: dspy.Image, target_language: str -> generated_code: str",
+ "inputs": {"ui_image": "https://example.com/button.png", "target_language": "HTML"},
+ "key_output": "generated_code",
+ "expected": {"generated_code": "<button>Click me</button>"},
+ },
+ {
+ "name": "bbox_detection",
+ "signature": "image: dspy.Image -> bboxes: List[Tuple[int, int, int, int]]",
+ "inputs": {"image": "https://example.com/image.jpg"},
+ "key_output": "bboxes",
+ "expected": {"bboxes": [(10, 20, 30, 40), (50, 60, 70, 80)]},
+ },
+ {
+ "name": "multilingual_caption",
+ "signature": "image: dspy.Image, languages: List[str] -> captions: Dict[str, str]",
+ "inputs": {"image": "https://example.com/dog.jpg", "languages": ["en", "es", "fr"]},
+ "key_output": "captions",
+ "expected": {
+ "captions": {"en": "A golden retriever", "es": "Un golden retriever", "fr": "Un golden retriever"}
+ },
+ },
+ ],
+)
def test_basic_image_operations(test_case):
"""Consolidated test for basic image operations"""
predictor, lm = setup_predictor(test_case["signature"], test_case["expected"])
-
+
# Convert string URLs to dspy.Image objects
- inputs = {k: dspy.Image.from_url(v) if isinstance(v, str) and k in ["image", "ui_image"] else v
- for k, v in test_case["inputs"].items()}
-
+ inputs = {
+ k: dspy.Image.from_url(v) if isinstance(v, str) and k in ["image", "ui_image"] else v
+ for k, v in test_case["inputs"].items()
+ }
+
result = predictor(**inputs)
-
+
# Check result based on output field name
- output_field = next(f for f in ["probabilities", "generated_code", "bboxes", "captions"]
- if hasattr(result, f))
+ output_field = next(f for f in ["probabilities", "generated_code", "bboxes", "captions"] if hasattr(result, f))
assert getattr(result, output_field) == test_case["expected"][test_case["key_output"]]
assert count_messages_with_image_url_pattern(lm.history[-1]["messages"]) == 1
[email protected]("image_input,description", [
- ("pil_image", "PIL Image"),
- ("encoded_pil_image", "encoded PIL image string"),
- ("dspy_image_download", "dspy.Image with download=True"),
- ("dspy_image_no_download", "dspy.Image without download")
-])
-def test_image_input_formats(request, sample_pil_image, sample_dspy_image_download,
- sample_dspy_image_no_download, image_input, description):
+
+ "image_input,description",
+ [
+ ("pil_image", "PIL Image"),
+ ("encoded_pil_image", "encoded PIL image string"),
+ ("dspy_image_download", "dspy.Image with download=True"),
+ ("dspy_image_no_download", "dspy.Image without download"),
+ ],
+)
+def test_image_input_formats(
+ request, sample_pil_image, sample_dspy_image_download, sample_dspy_image_no_download, image_input, description
+):
"""Test different input formats for image fields"""
signature = "image: dspy.Image, class_labels: List[str] -> probabilities: Dict[str, float]"
expected = {"probabilities": {"dog": 0.8, "cat": 0.1, "bird": 0.1}}
@@ -135,9 +147,9 @@ def test_image_input_formats(request, sample_pil_image, sample_dspy_image_downlo
"pil_image": sample_pil_image,
"encoded_pil_image": encode_image(sample_pil_image),
"dspy_image_download": sample_dspy_image_download,
- "dspy_image_no_download": sample_dspy_image_no_download
+ "dspy_image_no_download": sample_dspy_image_no_download,
}
-
+
actual_input = input_map[image_input]
# TODO(isaacbmiller): Support the cases without direct dspy.Image coercion
if image_input in ["pil_image", "encoded_pil_image"]:
@@ -147,6 +159,7 @@ def test_image_input_formats(request, sample_pil_image, sample_dspy_image_downlo
assert result.probabilities == expected["probabilities"]
assert count_messages_with_image_url_pattern(lm.history[-1]["messages"]) == 1
+
def test_predictor_save_load(sample_url, sample_pil_image):
"""Test saving and loading predictors with image fields"""
signature = "image: dspy.Image -> caption: str"
@@ -154,29 +167,30 @@ def test_predictor_save_load(sample_url, sample_pil_image):
dspy.Example(image=dspy.Image.from_url(sample_url), caption="Example 1"),
dspy.Example(image=sample_pil_image, caption="Example 2"),
]
-
+
predictor, lm = setup_predictor(signature, {"caption": "A golden retriever"})
optimizer = dspy.teleprompt.LabeledFewShot(k=1)
compiled_predictor = optimizer.compile(student=predictor, trainset=examples, sample=False)
- with tempfile.NamedTemporaryFile(mode='w+', delete=True, suffix=".json") as temp_file:
+ with tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".json") as temp_file:
compiled_predictor.save(temp_file.name)
loaded_predictor = dspy.Predict(signature)
loaded_predictor.load(temp_file.name)
-
- result = loaded_predictor(image=dspy.Image.from_url("https://example.com/dog.jpg"))
+
+ loaded_predictor(image=dspy.Image.from_url("https://example.com/dog.jpg"))
assert count_messages_with_image_url_pattern(lm.history[-1]["messages"]) == 2
assert "<DSPY_IMAGE_START>" not in str(lm.history[-1]["messages"])
+
def test_save_load_complex_default_types():
"""Test saving and loading predictors with complex default types (lists of images)"""
examples = [
dspy.Example(
image_list=[
dspy.Image.from_url("https://example.com/dog.jpg"),
- dspy.Image.from_url("https://example.com/cat.jpg")
+ dspy.Image.from_url("https://example.com/cat.jpg"),
],
- caption="Example 1"
+ caption="Example 1",
).with_inputs("image_list"),
]
@@ -188,50 +202,54 @@ def test_save_load_complex_default_types():
optimizer = dspy.teleprompt.LabeledFewShot(k=1)
compiled_predictor = optimizer.compile(student=predictor, trainset=examples, sample=False)
- with tempfile.NamedTemporaryFile(mode='w+', delete=True, suffix=".json") as temp_file:
+ with tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".json") as temp_file:
compiled_predictor.save(temp_file.name)
loaded_predictor = dspy.Predict(ComplexTypeSignature)
loaded_predictor.load(temp_file.name)
-
+
result = loaded_predictor(**examples[0].inputs())
assert result.caption == "A list of images"
assert str(lm.history[-1]["messages"]).count("'url'") == 4
assert "<DSPY_IMAGE_START>" not in str(lm.history[-1]["messages"])
+
class BasicImageSignature(dspy.Signature):
"""Basic signature with a single image input"""
+
image: dspy.Image = dspy.InputField()
output: str = dspy.OutputField()
+
class ImageListSignature(dspy.Signature):
"""Signature with a list of images input"""
+
image_list: List[dspy.Image] = dspy.InputField()
output: str = dspy.OutputField()
[email protected]("test_case", [
- {
- "name": "basic_dspy_signature",
- "signature_class": BasicImageSignature,
- "inputs": {
- "image": "https://example.com/dog.jpg"
+
+ "test_case",
+ [
+ {
+ "name": "basic_dspy_signature",
+ "signature_class": BasicImageSignature,
+ "inputs": {"image": "https://example.com/dog.jpg"},
+ "expected": {"output": "A dog photo"},
+ "expected_image_urls": 2,
},
- "expected": {"output": "A dog photo"},
- "expected_image_urls": 2
- },
- {
- "name": "list_dspy_signature",
- "signature_class": ImageListSignature,
- "inputs": {
- "image_list": ["https://example.com/dog.jpg", "https://example.com/cat.jpg"]
+ {
+ "name": "list_dspy_signature",
+ "signature_class": ImageListSignature,
+ "inputs": {"image_list": ["https://example.com/dog.jpg", "https://example.com/cat.jpg"]},
+ "expected": {"output": "Multiple photos"},
+ "expected_image_urls": 4,
},
- "expected": {"output": "Multiple photos"},
- "expected_image_urls": 4
- }
-])
+ ],
+)
def test_save_load_complex_types(test_case):
"""Test saving and loading predictors with complex types"""
signature_cls = test_case["signature_class"]
-
+
# Convert string URLs to dspy.Image objects in input
processed_input = {}
for key, value in test_case["inputs"].items():
@@ -241,35 +259,35 @@ def test_save_load_complex_types(test_case):
processed_input[key] = [dspy.Image.from_url(url) for url in value]
else:
processed_input[key] = value
-
+
# Create example and predictor
- examples = [
- dspy.Example(**processed_input, **test_case["expected"]).with_inputs(*processed_input.keys())
- ]
-
+ examples = [dspy.Example(**processed_input, **test_case["expected"]).with_inputs(*processed_input.keys())]
+
predictor, lm = setup_predictor(signature_cls, test_case["expected"])
optimizer = dspy.teleprompt.LabeledFewShot(k=1)
compiled_predictor = optimizer.compile(student=predictor, trainset=examples, sample=False)
-
+
# Test save and load
- with tempfile.NamedTemporaryFile(mode='w+', delete=True, suffix=".json") as temp_file:
+ with tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".json") as temp_file:
compiled_predictor.save(temp_file.name)
loaded_predictor = dspy.Predict(signature_cls)
loaded_predictor.load(temp_file.name)
-
+
# Run prediction
result = loaded_predictor(**processed_input)
-
+
# Verify output matches expected
for key, value in test_case["expected"].items():
assert getattr(result, key) == value
-
+
# Verify correct number of image URLs in messages
assert count_messages_with_image_url_pattern(lm.history[-1]["messages"]) == test_case["expected_image_urls"]
assert "<DSPY_IMAGE_START>" not in str(lm.history[-1]["messages"])
+
def test_save_load_pydantic_model():
"""Test saving and loading predictors with pydantic models"""
+
class ImageModel(pydantic.BaseModel):
image: dspy.Image
image_list: Optional[List[dspy.Image]] = None
@@ -283,20 +301,18 @@ def test_save_load_pydantic_model():
model_input = ImageModel(
image=dspy.Image.from_url("https://example.com/dog.jpg"),
image_list=[dspy.Image.from_url("https://example.com/cat.jpg")],
- output="Multiple photos"
+ output="Multiple photos",
)
# Create example and predictor
- examples = [
- dspy.Example(model_input=model_input, output="Multiple photos").with_inputs("model_input")
- ]
+ examples = [dspy.Example(model_input=model_input, output="Multiple photos").with_inputs("model_input")]
predictor, lm = setup_predictor(PydanticSignature, {"output": "Multiple photos"})
optimizer = dspy.teleprompt.LabeledFewShot(k=1)
compiled_predictor = optimizer.compile(student=predictor, trainset=examples, sample=False)
# Test save and load
- with tempfile.NamedTemporaryFile(mode='w+', delete=True, suffix=".json") as temp_file:
+ with tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".json") as temp_file:
compiled_predictor.save(temp_file.name)
loaded_predictor = dspy.Predict(PydanticSignature)
loaded_predictor.load(temp_file.name)
@@ -309,8 +325,10 @@ def test_save_load_pydantic_model():
assert count_messages_with_image_url_pattern(lm.history[-1]["messages"]) == 4
assert "<DSPY_IMAGE_START>" not in str(lm.history[-1]["messages"])
+
def test_optional_image_field():
"""Test that optional image fields are not required"""
+
class OptionalImageSignature(dspy.Signature):
image: Optional[dspy.Image] = dspy.InputField()
output: str = dspy.OutputField()
@@ -424,7 +442,7 @@ def test_pdf_from_file():
# Clean up the temporary file
try:
os.unlink(tmp_file_path)
- except:
+ except Exception:
pass
@@ -433,9 +451,9 @@ def test_image_repr():
url_image = dspy.Image.from_url("https://example.com/dog.jpg", download=False)
assert str(url_image) == "<DSPY_IMAGE_START>https://example.com/dog.jpg<DSPY_IMAGE_END>"
assert repr(url_image) == "Image(url='https://example.com/dog.jpg')"
-
- sample_pil = PILImage.new('RGB', (60, 30), color='red')
+
+ sample_pil = PILImage.new("RGB", (60, 30), color="red")
pil_image = dspy.Image.from_PIL(sample_pil)
assert str(pil_image).startswith("<DSPY_IMAGE_START>data:image/png;base64,")
assert str(pil_image).endswith("<DSPY_IMAGE_END>")
- assert "base64" in str(pil_image)
\ No newline at end of file
+ assert "base64" in str(pil_image)
diff --git a/tests/signatures/test_signature.py b/tests/signatures/test_signature.py
index 800057069..564a74906 100644
--- a/tests/signatures/test_signature.py
+++ b/tests/signatures/test_signature.py
@@ -1,4 +1,3 @@
-import textwrap
from typing import Any, Dict, List, Optional, Tuple, Union
import pydantic
@@ -93,7 +92,7 @@ def test_signature_instructions():
def test_signature_instructions_none():
sig1 = Signature("a, b -> c")
- assert sig1.instructions == f"Given the fields `a`, `b`, produce the fields `c`."
+ assert sig1.instructions == "Given the fields `a`, `b`, produce the fields `c`."
def test_signature_from_dict():
@@ -133,15 +132,15 @@ def test_insert_field_at_various_positions():
input1: str = InputField()
output1: int = OutputField()
- S1 = InitialSignature.prepend("new_input_start", InputField(), str)
- S2 = InitialSignature.append("new_input_end", InputField(), str)
- assert "new_input_start" == list(S1.input_fields.keys())[0]
- assert "new_input_end" == list(S2.input_fields.keys())[-1]
+ s1 = InitialSignature.prepend("new_input_start", InputField(), str)
+ s2 = InitialSignature.append("new_input_end", InputField(), str)
+ assert "new_input_start" == list(s1.input_fields.keys())[0] # noqa: RUF015
+ assert "new_input_end" == list(s2.input_fields.keys())[-1]
- S3 = InitialSignature.prepend("new_output_start", OutputField(), str)
- S4 = InitialSignature.append("new_output_end", OutputField(), str)
- assert "new_output_start" == list(S3.output_fields.keys())[0]
- assert "new_output_end" == list(S4.output_fields.keys())[-1]
+ s3 = InitialSignature.prepend("new_output_start", OutputField(), str)
+ s4 = InitialSignature.append("new_output_end", OutputField(), str)
+ assert "new_output_start" == list(s3.output_fields.keys())[0] # noqa: RUF015
+ assert "new_output_end" == list(s4.output_fields.keys())[-1]
def test_order_preserved_with_mixed_annotations():
@@ -356,7 +355,7 @@ def test_typed_signatures_complex_combinations():
# inner_union should be Optional[Tuple[int, str]]
# which is Union[Tuple[int, str], None]
assert getattr(inner_union, "__origin__", None) is Union
- tuple_type = [t for t in inner_union.__args__ if t != type(None)][0]
+ tuple_type = [t for t in inner_union.__args__ if t != type(None)][0] # noqa: RUF015
assert getattr(tuple_type, "__origin__", None) is tuple
assert tuple_type.__args__ == (int, str)
diff --git a/tests/streaming/test_streaming.py b/tests/streaming/test_streaming.py
index 1f89fc229..9363f23cb 100644
--- a/tests/streaming/test_streaming.py
+++ b/tests/streaming/test_streaming.py
@@ -1,9 +1,9 @@
import os
+
import pytest
import dspy
from dspy.streaming import StatusMessage, StatusMessageProvider, streaming_response
-from tests.test_utils.server import litellm_test_server
@pytest.mark.anyio
@@ -102,7 +102,7 @@ async def test_custom_status_streaming():
class MyStatusMessageProvider(StatusMessageProvider):
def tool_start_status_message(self, instance, inputs):
- return f"Tool starting!"
+ return "Tool starting!"
def tool_end_status_message(self, outputs):
return "Tool finished!"
diff --git a/tests/teleprompt/test_bootstrap.py b/tests/teleprompt/test_bootstrap.py
index a112947a4..17b58aeda 100644
--- a/tests/teleprompt/test_bootstrap.py
+++ b/tests/teleprompt/test_bootstrap.py
@@ -1,5 +1,3 @@
-import textwrap
-
import pytest
import dspy
diff --git a/tests/teleprompt/test_ensemble.py b/tests/teleprompt/test_ensemble.py
index 54ca2708d..c779e8782 100644
--- a/tests/teleprompt/test_ensemble.py
+++ b/tests/teleprompt/test_ensemble.py
@@ -1,4 +1,5 @@
import pytest
+
import dspy
from dspy.teleprompt import Ensemble
@@ -46,9 +47,7 @@ def test_ensemble_with_size_limitation():
ensembled_program = ensemble.compile(programs)
outputs = ensembled_program()
- assert (
- len(outputs) == ensemble_size
- ), "Ensemble did not respect the specified size limitation"
+ assert len(outputs) == ensemble_size, "Ensemble did not respect the specified size limitation"
def test_ensemble_deterministic_behavior():
diff --git a/tests/teleprompt/test_finetune.py b/tests/teleprompt/test_finetune.py
index f87f5c14c..464090415 100644
--- a/tests/teleprompt/test_finetune.py
+++ b/tests/teleprompt/test_finetune.py
@@ -1 +1 @@
-# TODO
\ No newline at end of file
+# TODO
diff --git a/tests/teleprompt/test_grpo.py b/tests/teleprompt/test_grpo.py
index 9de74f7c5..0c2e61b50 100644
--- a/tests/teleprompt/test_grpo.py
+++ b/tests/teleprompt/test_grpo.py
@@ -1,9 +1,10 @@
from dspy.teleprompt.grpo import GRPO
+
def test_grpo_dataset_shuffler():
- dataset = [1,2,3]
+ dataset = [1, 2, 3]
grpo = GRPO(
- num_dspy_examples_per_grpo_step=3,
+ num_dspy_examples_per_grpo_step=3,
exclude_demos=True,
)
@@ -12,9 +13,10 @@ def test_grpo_dataset_shuffler():
trainset_instances.append(grpo.select_training_sample_and_update_shuffled_trainset(dataset, i))
assert len(trainset_instances[-1]) == 3
assert set(trainset_instances[-1]) == set(dataset)
-
+
+
def test_grpo_dataset_shuffler_with_num_ex_per_step_less_dataset():
- dataset = [1,2,3]
+ dataset = [1, 2, 3]
grpo = GRPO(
num_dspy_examples_per_grpo_step=2,
exclude_demos=True,
@@ -24,20 +26,22 @@ def test_grpo_dataset_shuffler_with_num_ex_per_step_less_dataset():
for i in range(15):
trainset_instances.append(grpo.select_training_sample_and_update_shuffled_trainset(dataset, i))
assert len(trainset_instances[-1]) == 2
-
+
from collections import Counter
+
counter = Counter()
for instance in trainset_instances:
counter.update(instance)
-
+
assert len(counter) == 3
for i in counter:
assert counter[i] == 10
+
def test_grpo_dataset_shuffler_with_num_ex_per_step_greater_dataset():
- dataset = [1,2,3]
+ dataset = [1, 2, 3]
grpo = GRPO(
- num_dspy_examples_per_grpo_step=5,
+ num_dspy_examples_per_grpo_step=5,
exclude_demos=True,
)
@@ -45,18 +49,20 @@ def test_grpo_dataset_shuffler_with_num_ex_per_step_greater_dataset():
for i in range(6):
trainset_instances.append(grpo.select_training_sample_and_update_shuffled_trainset(dataset, i))
assert len(trainset_instances[-1]) == 5
-
+
from collections import Counter
+
counter = Counter()
for instance in trainset_instances:
counter.update(instance)
-
+
assert len(counter) == 3
for i in counter:
assert counter[i] == 10
+
if __name__ == "__main__":
test_grpo_dataset_shuffler()
test_grpo_dataset_shuffler_with_num_ex_per_step_less_dataset()
test_grpo_dataset_shuffler_with_num_ex_per_step_greater_dataset()
- print("All tests passed!")
\ No newline at end of file
+ print("All tests passed!")
diff --git a/tests/teleprompt/test_teleprompt.py b/tests/teleprompt/test_teleprompt.py
index df183285d..4b952fc79 100644
--- a/tests/teleprompt/test_teleprompt.py
+++ b/tests/teleprompt/test_teleprompt.py
@@ -1,5 +1,6 @@
from dspy.teleprompt.teleprompt import Teleprompter
+
class DummyTeleprompter(Teleprompter):
def __init__(self, param1: int, param2: str):
super().__init__()
@@ -13,4 +14,4 @@ class DummyTeleprompter(Teleprompter):
def test_get_params():
teleprompter = DummyTeleprompter(param1=1, param2="test")
params = teleprompter.get_params()
- assert params == {"param1": 1, "param2": "test"}
\ No newline at end of file
+ assert params == {"param1": 1, "param2": "test"}
diff --git a/tests/teleprompt/test_utils.py b/tests/teleprompt/test_utils.py
index e6f2d8a39..5b3a59098 100644
--- a/tests/teleprompt/test_utils.py
+++ b/tests/teleprompt/test_utils.py
@@ -1,9 +1,11 @@
from unittest.mock import Mock
+
import pytest
import dspy
from dspy.teleprompt.utils import eval_candidate_program
+
class DummyModule(dspy.Module):
def __init__(self):
super().__init__()
@@ -22,10 +24,11 @@ def test_eval_candidate_program_full_trainset():
evaluate.assert_called_once()
_, called_kwargs = evaluate.call_args
- assert len(called_kwargs['devset']) == len(trainset)
- assert called_kwargs['callback_metadata'] == {"metric_key": "eval_full"}
+ assert len(called_kwargs["devset"]) == len(trainset)
+ assert called_kwargs["callback_metadata"] == {"metric_key": "eval_full"}
assert result == 0
+
def test_eval_candidate_program_minibatch():
trainset = [1, 2, 3, 4, 5]
candidate_program = DummyModule()
@@ -36,10 +39,11 @@ def test_eval_candidate_program_minibatch():
evaluate.assert_called_once()
_, called_kwargs = evaluate.call_args
- assert len(called_kwargs['devset']) == batch_size
- assert called_kwargs['callback_metadata'] == {"metric_key": "eval_minibatch"}
+ assert len(called_kwargs["devset"]) == batch_size
+ assert called_kwargs["callback_metadata"] == {"metric_key": "eval_minibatch"}
assert result == 0
+
@pytest.mark.parametrize("return_all_scores", [True, False])
def test_eval_candidate_program_failure(return_all_scores):
trainset = [1, 2, 3, 4, 5]
@@ -47,9 +51,11 @@ def test_eval_candidate_program_failure(return_all_scores):
evaluate = Mock(side_effect=ValueError("Error"))
batch_size = 3
- result = eval_candidate_program(batch_size, trainset, candidate_program, evaluate, return_all_scores=return_all_scores)
+ result = eval_candidate_program(
+ batch_size, trainset, candidate_program, evaluate, return_all_scores=return_all_scores
+ )
if return_all_scores:
- assert result == (0.0, [0.0]*len(trainset))
+ assert result == (0.0, [0.0] * len(trainset))
else:
assert result == 0.0
diff --git a/tests/test_utils/server/__init__.py b/tests/test_utils/server/__init__.py
index 84785e606..0866da06e 100644
--- a/tests/test_utils/server/__init__.py
+++ b/tests/test_utils/server/__init__.py
@@ -56,7 +56,7 @@ def read_litellm_test_server_request_logs(server_log_file_path: str) -> List[Dic
A list of log entries, where each entry corresponds to one request handled by the server.
"""
data = []
- with open(server_log_file_path, "r") as f:
+ with open(server_log_file_path) as f:
for line in f:
data.append(json.loads(line))
diff --git a/tests/test_utils/server/litellm_server.py b/tests/test_utils/server/litellm_server.py
index 58801e9e7..4fe7a8c22 100644
--- a/tests/test_utils/server/litellm_server.py
+++ b/tests/test_utils/server/litellm_server.py
@@ -1,6 +1,5 @@
import json
import os
-import time
from typing import AsyncIterator, Iterator
import litellm
@@ -72,7 +71,7 @@ def _append_request_to_log_file(completion_kwargs):
log_file_path = os.environ.get(LITELLM_TEST_SERVER_LOG_FILE_PATH_ENV_VAR)
if log_file_path is None:
raise ValueError(
- f"Server logs file path is not defined! Please set the path using the"
+ "Server logs file path is not defined! Please set the path using the"
+ f" {LITELLM_TEST_SERVER_LOG_FILE_PATH_ENV_VAR} environment variable."
)
diff --git a/tests/utils/test_asyncify.py b/tests/utils/test_asyncify.py
index 37e268bd8..ffe959042 100644
--- a/tests/utils/test_asyncify.py
+++ b/tests/utils/test_asyncify.py
@@ -1,7 +1,7 @@
-from time import time, sleep
import asyncio
-
import math
+from time import sleep, time
+
import pytest
import dspy
@@ -51,5 +51,3 @@ async def test_asyncify():
await verify_asyncify(4, 10)
await verify_asyncify(8, 15)
await verify_asyncify(8, 30)
-
-
diff --git a/tests/utils/test_exceptions.py b/tests/utils/test_exceptions.py
index b3b63ea41..2f852523d 100644
--- a/tests/utils/test_exceptions.py
+++ b/tests/utils/test_exceptions.py
@@ -1,7 +1,5 @@
-import pytest
-from dspy.utils.exceptions import AdapterParseError
-from dspy.signatures.signature import Signature
import dspy
+from dspy.utils.exceptions import AdapterParseError
def test_adapter_parse_error_basic():
diff --git a/tests/utils/test_mcp.py b/tests/utils/test_mcp.py
index 07cf49d86..ab11682f1 100644
--- a/tests/utils/test_mcp.py
+++ b/tests/utils/test_mcp.py
@@ -1,6 +1,6 @@
-import pytest
import asyncio
+import pytest
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
diff --git a/tests/utils/test_parallelizer.py b/tests/utils/test_parallelizer.py
index f1ec3c801..28307e4ea 100644
--- a/tests/utils/test_parallelizer.py
+++ b/tests/utils/test_parallelizer.py
@@ -1,5 +1,7 @@
import time
+
import pytest
+
from dspy.utils.parallelizer import ParallelExecutor
diff --git a/tests/utils/test_saving.py b/tests/utils/test_saving.py
index 6b28c942c..2f6cc40f8 100644
--- a/tests/utils/test_saving.py
+++ b/tests/utils/test_saving.py
@@ -1,9 +1,8 @@
+import logging
+from unittest.mock import patch
+
import dspy
from dspy.utils import DummyLM
-from unittest.mock import patch
-import pytest
-from dspy.utils.saving import get_dependency_versions
-import logging
def test_save_predict(tmp_path):
diff --git a/tests/utils/test_unbatchify.py b/tests/utils/test_unbatchify.py
index 685519c58..473f06027 100644
--- a/tests/utils/test_unbatchify.py
+++ b/tests/utils/test_unbatchify.py
@@ -1,6 +1,6 @@
import time
-from unittest.mock import MagicMock
from concurrent.futures import Future
+from unittest.mock import MagicMock
from dspy.utils.unbatchify import Unbatchify
@@ -50,7 +50,6 @@ def test_unbatchify_batch_size_trigger():
unbatcher.close()
-
def test_unbatchify_timeout_trigger():
"""Test that the batch processes after max_wait_time."""
batch_fn_mock = MagicMock(wraps=simple_batch_processor)
@@ -71,5 +70,3 @@ def test_unbatchify_timeout_trigger():
assert results == [101, 201]
unbatcher.close()
-
-
diff --git a/tests/utils/test_usage_tracker.py b/tests/utils/test_usage_tracker.py
index a9accef64..2ce921147 100644
--- a/tests/utils/test_usage_tracker.py
+++ b/tests/utils/test_usage_tracker.py
@@ -1,9 +1,9 @@
+import os
+
import pytest
+
import dspy
-from unittest.mock import Mock, patch, MagicMock
from dspy.utils.usage_tracker import UsageTracker, track_usage
-from dspy.utils.dummies import DummyLM
-import os
def test_add_usage_entry():