Skip to content

Commit b406ea0

Browse files
committed
ruff
1 parent 4210d62 commit b406ea0

File tree

6 files changed

+24
-25
lines changed

6 files changed

+24
-25
lines changed

dspy/adapters/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
from dspy.adapters.baml_adapter import BAMLAdapter
12
from dspy.adapters.base import Adapter
23
from dspy.adapters.chat_adapter import ChatAdapter
34
from dspy.adapters.json_adapter import JSONAdapter
45
from dspy.adapters.two_step_adapter import TwoStepAdapter
5-
from dspy.adapters.baml_adapter import BAMLAdapter
66
from dspy.adapters.types import Audio, Code, History, Image, Tool, ToolCalls, Type
77
from dspy.adapters.xml_adapter import XMLAdapter
88

dspy/adapters/types/base_type.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def format(self) -> list[dict[str, Any]]:
2525
return [{"type": "image_url", "image_url": {"url": self.url}}]
2626
```
2727
"""
28-
28+
2929
def __class_getitem__(cls, item):
3030
# Support generics while preserving custom instance check
3131
result = super().__class_getitem__(item)
@@ -37,13 +37,13 @@ def __instancecheck__(cls, instance):
3737
# First check normal instance check
3838
if super(Type, cls).__instancecheck__(instance):
3939
return True
40-
40+
4141
# Check if instance is a string with custom type format
4242
# Thoretically we want types to implement their own data validation for whatever comes in the middle
4343
if isinstance(instance, str):
4444
pattern = rf"{CUSTOM_TYPE_START_IDENTIFIER}(.*?){CUSTOM_TYPE_END_IDENTIFIER}"
4545
return bool(re.search(pattern, instance))
46-
46+
4747
return False
4848

4949
def format(self) -> list[dict[str, Any]] | str:

dspy/adapters/types/image.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pydantic
1212
import requests
1313

14-
from dspy.adapters.types.base_type import Type, CUSTOM_TYPE_START_IDENTIFIER, CUSTOM_TYPE_END_IDENTIFIER
14+
from dspy.adapters.types.base_type import CUSTOM_TYPE_END_IDENTIFIER, CUSTOM_TYPE_START_IDENTIFIER, Type
1515

1616
try:
1717
from PIL import Image as PILImage
@@ -240,26 +240,26 @@ def _extract_url_from_custom_type(text: str) -> str:
240240
"""Extract the image URL from custom type format string."""
241241
pattern = rf"{CUSTOM_TYPE_START_IDENTIFIER}(.*?){CUSTOM_TYPE_END_IDENTIFIER}"
242242
match = re.search(pattern, text, re.DOTALL)
243-
243+
244244
if not match:
245245
raise ValueError("No custom type format found in string")
246-
246+
247247
custom_content = match.group(1).strip()
248-
248+
249249
try:
250250
# Try to parse as JSON
251251
parsed = json.loads(custom_content.replace("'", '"'))
252-
252+
253253
# Extract URL from the image_url structure
254254
if isinstance(parsed, list) and len(parsed) > 0:
255255
first_item = parsed[0]
256256
if isinstance(first_item, dict) and first_item.get("type") == "image_url":
257257
image_url_dict = first_item.get("image_url", {})
258258
if "url" in image_url_dict:
259259
return image_url_dict["url"]
260-
260+
261261
raise ValueError("Could not find image URL in custom type format")
262-
262+
263263
except json.JSONDecodeError:
264264
raise ValueError(f"Invalid JSON in custom type format: {custom_content}")
265265

dspy/adapters/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
import json
55
import types
66
from collections.abc import Mapping
7+
from copy import deepcopy
78
from typing import Any, Literal, Union, get_args, get_origin
89

910
import json_repair
10-
from litellm.utils import f
1111
import pydantic
1212
from pydantic import TypeAdapter
1313
from pydantic.fields import FieldInfo
14-
from copy import deepcopy
1514

1615
from dspy.adapters.types.base_type import Type as DspyType
1716
from dspy.signatures.utils import get_dspy_field_type
@@ -55,7 +54,7 @@ def format_field_value(field_info: FieldInfo, value: Any, assume_text=True, is_p
5554
# If the annotation is a Type subclass, but the value is not a instance of that type, force it to be one.
5655
if issubclass(field_info.annotation, Type) and not isinstance(value, field_info.annotation) and not is_placeholder:
5756
value = field_info.annotation(value)
58-
57+
5958
if isinstance(value, list) and field_info.annotation is str:
6059
# If the field has no special type requirements, format it as a nice numbered list for the LM.
6160
string_value = _format_input_list_field_value(value)

dspy/utils/dummies.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
import numpy as np
66

7-
from dspy.adapters.chat_adapter import ChatAdapter, FieldInfoWithName, field_header_pattern
7+
from dspy.adapters.chat_adapter import FieldInfoWithName, field_header_pattern
88
from dspy.clients.lm import LM
9+
from dspy.dsp.utils.settings import settings
910
from dspy.dsp.utils.utils import dotdict
1011
from dspy.signatures.field import OutputField
1112
from dspy.utils.callback import with_callbacks
12-
from dspy.dsp.utils.settings import settings
1313

1414

1515
class DummyLM(LM):
@@ -105,7 +105,7 @@ def format_answer_fields(field_names_and_values: dict[str, Any]):
105105
# Fallback to ChatAdapter if no adapter is set
106106
from dspy.adapters.chat_adapter import ChatAdapter
107107
adapter = ChatAdapter()
108-
108+
109109
# Try to use role="assistant" if the adapter supports it (like JSONAdapter)
110110
try:
111111
return adapter.format_field_with_value(fields_with_values, role="assistant")

tests/signatures/test_adapter_image.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ def test_basic_image_operations(test_case):
129129
@pytest.mark.parametrize(
130130
"adapter_type",
131131
[
132-
"chat_adapter",
133-
"json_adapter",
134-
# "two_step_adapter",
135-
"baml_adapter",
132+
"chat_adapter",
133+
"json_adapter",
134+
# "two_step_adapter",
135+
"baml_adapter",
136136
"xml_adapter",
137137
],
138138
)
@@ -502,24 +502,24 @@ def test_from_methods_warn(tmp_path):
502502
def test_invalid_string_format():
503503
"""Test that invalid string formats raise a ValueError"""
504504
invalid_string = "this_is_not_a_url_or_file"
505-
505+
506506
# Should raise a ValueError and not pass the string through
507507
with pytest.raises(ValueError, match="Unrecognized") as warning_info:
508508
image = dspy.Image(invalid_string)
509509

510510
def test_pil_image_with_download_parameter():
511511
"""Test behavior when PIL image is passed with download=True"""
512512
sample_pil = PILImage.new("RGB", (60, 30), color="red")
513-
513+
514514
# PIL image should be encoded regardless of download parameter
515515
image_no_download = dspy.Image(sample_pil)
516516
image_with_download = dspy.Image(sample_pil, download=True)
517-
517+
518518
# Both should result in base64 encoded data URIs
519519
assert image_no_download.url.startswith("data:")
520520
assert image_with_download.url.startswith("data:")
521521
assert "base64," in image_no_download.url
522522
assert "base64," in image_with_download.url
523-
523+
524524
# They should be identical since PIL images are always encoded
525525
assert image_no_download.url == image_with_download.url

0 commit comments

Comments
 (0)