Skip to content

Commit 1697f15

Browse files
authored
Drop Python 3.9 support (#8456)
* drop 3.9 support * lint * fix type for ChainOfThought
1 parent 4d19c3e commit 1697f15

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+321
-325
lines changed

.github/workflows/precommits_check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
- name: Set up Python
1111
uses: actions/setup-python@v4
1212
with:
13-
python-version: "3.9"
13+
python-version: "3.10"
1414
cache: "pip"
1515
- name: Check Pull Request Title
1616
uses: Slashgear/action-check-pr-title@main

dspy/adapters/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import TYPE_CHECKING, Any, Optional, Type, get_origin
2+
from typing import TYPE_CHECKING, Any, Type, get_origin
33

44
import json_repair
55
import litellm
@@ -17,7 +17,7 @@
1717

1818

1919
class Adapter:
20-
def __init__(self, callbacks: Optional[list[BaseCallback]] = None):
20+
def __init__(self, callbacks: list[BaseCallback] | None = None):
2121
self.callbacks = callbacks or []
2222

2323
def __init_subclass__(cls, **kwargs) -> None:
@@ -281,7 +281,7 @@ def format_assistant_message_content(
281281
self,
282282
signature: Type[Signature],
283283
outputs: dict[str, Any],
284-
missing_field_message: Optional[str] = None,
284+
missing_field_message: str | None = None,
285285
) -> str:
286286
"""Format the assistant message content.
287287

dspy/adapters/chat_adapter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22
import textwrap
3-
from typing import Any, Dict, NamedTuple, Optional, Type
3+
from typing import Any, Dict, NamedTuple, Type
44

55
from litellm import ContextWindowExceededError
66
from pydantic.fields import FieldInfo
@@ -27,7 +27,7 @@ class FieldInfoWithName(NamedTuple):
2727

2828

2929
class ChatAdapter(Adapter):
30-
def __init__(self, callbacks: Optional[list[BaseCallback]] = None):
30+
def __init__(self, callbacks: list[BaseCallback] | None = None):
3131
super().__init__(callbacks)
3232

3333
def __call__(

dspy/adapters/two_step_adapter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Optional, Type
1+
from typing import Any, Type
22

33
import json_repair
44

@@ -193,7 +193,7 @@ def format_assistant_message_content(
193193
self,
194194
signature: Type[Signature],
195195
outputs: dict[str, Any],
196-
missing_field_message: Optional[str] = None,
196+
missing_field_message: str | None = None,
197197
) -> str:
198198
parts = []
199199

dspy/adapters/types/base_type.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import re
3-
from typing import Any, Union, get_args, get_origin
3+
from typing import Any, get_args, get_origin
44

55
import json_repair
66
import pydantic
@@ -26,7 +26,7 @@ def format(self) -> list[dict[str, Any]]:
2626
```
2727
"""
2828

29-
def format(self) -> Union[list[dict[str, Any]], str]:
29+
def format(self) -> list[dict[str, Any]] | str:
3030
raise NotImplementedError
3131

3232
@classmethod

dspy/adapters/types/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Image(BaseType):
2828
"extra": "forbid",
2929
}
3030

31-
def format(self) -> Union[list[dict[str, Any]], str]:
31+
def format(self) -> list[dict[str, Any]] | str:
3232
try:
3333
image_url = encode_image(self.url)
3434
except Exception as e:

dspy/adapters/types/tool.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio
22
import inspect
3-
from typing import TYPE_CHECKING, Any, Callable, Optional, Tuple, Type, get_origin, get_type_hints
3+
from typing import TYPE_CHECKING, Any, Callable, Tuple, Type, get_origin, get_type_hints
44

55
from jsonschema import ValidationError, validate
66
from pydantic import BaseModel, TypeAdapter, create_model
@@ -24,21 +24,21 @@ class Tool(BaseType):
2424
"""
2525

2626
func: Callable
27-
name: Optional[str] = None
28-
desc: Optional[str] = None
29-
args: Optional[dict[str, Any]] = None
30-
arg_types: Optional[dict[str, Any]] = None
31-
arg_desc: Optional[dict[str, str]] = None
27+
name: str | None = None
28+
desc: str | None = None
29+
args: dict[str, Any] | None = None
30+
arg_types: dict[str, Any] | None = None
31+
arg_desc: dict[str, str] | None = None
3232
has_kwargs: bool = False
3333

3434
def __init__(
3535
self,
3636
func: Callable,
37-
name: Optional[str] = None,
38-
desc: Optional[str] = None,
39-
args: Optional[dict[str, Any]] = None,
40-
arg_types: Optional[dict[str, Any]] = None,
41-
arg_desc: Optional[dict[str, str]] = None,
37+
name: str | None = None,
38+
desc: str | None = None,
39+
args: dict[str, Any] | None = None,
40+
arg_types: dict[str, Any] | None = None,
41+
arg_desc: dict[str, str] | None = None,
4242
):
4343
"""Initialize the Tool class.
4444
@@ -71,7 +71,7 @@ def foo(x: int, y: str = "hello"):
7171
super().__init__(func=func, name=name, desc=desc, args=args, arg_types=arg_types, arg_desc=arg_desc)
7272
self._parse_function(func, arg_desc)
7373

74-
def _parse_function(self, func: Callable, arg_desc: Optional[dict[str, str]] = None):
74+
def _parse_function(self, func: Callable, arg_desc: dict[str, str] | None = None):
7575
"""Helper method that parses a function to extract the name, description, and args.
7676
7777
This is a helper function that automatically infers the name, description, and args of the tool from the

dspy/adapters/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def serialize_for_json(value: Any) -> Any:
3232
return str(value)
3333

3434

35-
def format_field_value(field_info: FieldInfo, value: Any, assume_text=True) -> Union[str, dict]:
35+
def format_field_value(field_info: FieldInfo, value: Any, assume_text=True) -> str | dict:
3636
"""
3737
Formats the value of the specified field according to the field's DSPy type (input or output),
3838
annotation (e.g. str, int, etc.), and the type of the value itself.

dspy/adapters/xml_adapter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from typing import Any, Dict, Optional, Type
2+
from typing import Any, Dict, Type
33

44
from dspy.adapters.chat_adapter import ChatAdapter, FieldInfoWithName
55
from dspy.adapters.utils import format_field_value
@@ -8,7 +8,7 @@
88

99

1010
class XMLAdapter(ChatAdapter):
11-
def __init__(self, callbacks: Optional[list[BaseCallback]] = None):
11+
def __init__(self, callbacks: list[BaseCallback] | None = None):
1212
super().__init__(callbacks)
1313
self.field_pattern = re.compile(r"<(?P<name>\w+)>((?P<content>.*?))</\1>", re.DOTALL)
1414

dspy/clients/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def _litellm_track_cache_hit_callback(kwargs, completion_response, start_time, e
2727

2828

2929
def configure_cache(
30-
enable_disk_cache: Optional[bool] = True,
31-
enable_memory_cache: Optional[bool] = True,
32-
disk_cache_dir: Optional[str] = DISK_CACHE_DIR,
33-
disk_size_limit_bytes: Optional[int] = DISK_CACHE_LIMIT,
34-
memory_max_entries: Optional[int] = 1000000,
30+
enable_disk_cache: bool | None = True,
31+
enable_memory_cache: bool | None = True,
32+
disk_cache_dir: str | None = DISK_CACHE_DIR,
33+
disk_size_limit_bytes: int | None = DISK_CACHE_LIMIT,
34+
memory_max_entries: int | None = 1000000,
3535
enable_litellm_cache: bool = False,
3636
):
3737
"""Configure the cache for DSPy.

0 commit comments

Comments
 (0)