Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ repos:
args: ["--maxkb=1024"]
- id: check-merge-conflict
- id: debug-statements

- repo: https://github.com/asottile/pyupgrade
rev: v3.20.0
hooks:
- id: pyupgrade
name: Enforce Python 3.10+ idioms
args: ["--py310-plus"]
2 changes: 1 addition & 1 deletion docs/scripts/generate_api_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def format_nav_section(nav_dict, indent_level=2):

def read_mkdocs_sections(filename: str = "mkdocs.yml"):
"""Read and parse the mkdocs.yml file into sections."""
with open(filename, "r") as f:
with open(filename) as f:
lines = f.readlines()

nav_start = -1
Expand Down
3 changes: 2 additions & 1 deletion dspy/adapters/types/tool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import inspect
from typing import TYPE_CHECKING, Any, Callable, get_origin, get_type_hints
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, get_origin, get_type_hints

import pydantic
from jsonschema import ValidationError, validate
Expand Down
3 changes: 2 additions & 1 deletion dspy/clients/embedding.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Callable
from collections.abc import Callable
from typing import Any

import litellm
import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion dspy/dsp/utils/dpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def __init__(self, **kwargs):
annotators: None or empty set (only tokenizes).
"""
self._regexp = regex.compile(
"(%s)|(%s)" % (self.ALPHA_NUM, self.NON_WS),
f"({self.ALPHA_NUM})|({self.NON_WS})",
flags=regex.IGNORECASE + regex.UNICODE + regex.MULTILINE,
)
if len(kwargs.get("annotators", {})) > 0:
Expand Down
3 changes: 2 additions & 1 deletion dspy/evaluate/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import json
import logging
import types
from typing import TYPE_CHECKING, Any, Callable
from collections.abc import Callable
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
import pandas as pd
Expand Down
2 changes: 1 addition & 1 deletion dspy/predict/best_of_n.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable
from collections.abc import Callable

import dspy
from dspy.predict.predict import Module, Prediction
Expand Down
2 changes: 1 addition & 1 deletion dspy/predict/code_act.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import inspect
import logging
from typing import Callable
from collections.abc import Callable

import dspy
from dspy.adapters.types.tool import Tool
Expand Down
3 changes: 2 additions & 1 deletion dspy/predict/react.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from typing import TYPE_CHECKING, Any, Callable, Literal
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, Literal

from litellm import ContextWindowExceededError

Expand Down
2 changes: 1 addition & 1 deletion dspy/predict/refine.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import inspect
import textwrap
from typing import Callable
from collections.abc import Callable

import orjson

Expand Down
1 change: 0 additions & 1 deletion dspy/retrievers/weaviate_rm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import dspy
from dspy.dsp.utils import dotdict
from dspy.primitives.prediction import Prediction
Expand Down
3 changes: 2 additions & 1 deletion dspy/streaming/streamify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import logging
import threading
from asyncio import iscoroutinefunction
from collections.abc import AsyncGenerator, Awaitable, Callable, Generator
from queue import Queue
from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, Generator
from typing import TYPE_CHECKING, Any

import litellm
import orjson
Expand Down
2 changes: 1 addition & 1 deletion dspy/teleprompt/avatar_optimizer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections.abc import Callable
from concurrent.futures import ThreadPoolExecutor
from copy import deepcopy
from random import sample
from typing import Callable

from pydantic import BaseModel
from tqdm import tqdm
Expand Down
2 changes: 1 addition & 1 deletion dspy/teleprompt/bettertogether.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import random
from typing import Callable
from collections.abc import Callable

import dspy
from dspy.primitives.example import Example
Expand Down
3 changes: 2 additions & 1 deletion dspy/teleprompt/bootstrap_finetune.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from collections import defaultdict
from typing import Any, Callable
from collections.abc import Callable
from typing import Any

import dspy
from dspy.adapters.base import Adapter
Expand Down
3 changes: 2 additions & 1 deletion dspy/teleprompt/bootstrap_trace.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
from collections.abc import Callable
from dataclasses import dataclass
from types import MethodType
from typing import Any, Callable, TypedDict
from typing import Any, TypedDict

import dspy
from dspy.evaluate.evaluate import Evaluate
Expand Down
3 changes: 2 additions & 1 deletion dspy/teleprompt/gepa/gepa_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import random
from typing import Any, Callable, Protocol, TypedDict
from collections.abc import Callable
from typing import Any, Protocol, TypedDict

from gepa import EvaluationBatch, GEPAAdapter
from gepa.core.adapter import ProposalFn
Expand Down
3 changes: 2 additions & 1 deletion dspy/teleprompt/grpo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
import random
from collections import Counter
from typing import Any, Callable, Literal
from collections.abc import Callable
from typing import Any, Literal

from dspy.adapters.base import Adapter
from dspy.adapters.chat_adapter import ChatAdapter
Expand Down
3 changes: 2 additions & 1 deletion dspy/teleprompt/mipro_optimizer_v2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
import random
from collections import defaultdict
from typing import TYPE_CHECKING, Any, Callable, Literal
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, Literal

import numpy as np

Expand Down
3 changes: 2 additions & 1 deletion dspy/teleprompt/simba.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import logging
import random
from typing import Any, Callable
from collections.abc import Callable
from typing import Any

import numpy as np

Expand Down
2 changes: 1 addition & 1 deletion dspy/teleprompt/simba_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
import logging
import textwrap
from typing import Callable
from collections.abc import Callable

import orjson

Expand Down
3 changes: 2 additions & 1 deletion dspy/utils/annotation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import inspect
import re
import types
from typing import Callable, ParamSpec, TypeVar, overload
from collections.abc import Callable
from typing import ParamSpec, TypeVar, overload

P = ParamSpec("P")
R = TypeVar("R")
Expand Down
3 changes: 2 additions & 1 deletion dspy/utils/asyncify.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import TYPE_CHECKING, Any, Awaitable, Callable
from collections.abc import Awaitable, Callable
from typing import TYPE_CHECKING, Any

import asyncer
from anyio import CapacityLimiter
Expand Down
3 changes: 2 additions & 1 deletion dspy/utils/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import inspect
import logging
import uuid
from collections.abc import Callable
from contextvars import ContextVar
from typing import Any, Callable
from typing import Any

import dspy

Expand Down
1 change: 0 additions & 1 deletion dspy/utils/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from dspy.signatures.signature import Signature


Expand Down
3 changes: 2 additions & 1 deletion dspy/utils/unbatchify.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import queue
import threading
import time
from collections.abc import Callable
from concurrent.futures import Future
from typing import Any, Callable
from typing import Any


class Unbatchify:
Expand Down
3 changes: 2 additions & 1 deletion dspy/utils/usage_tracker.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Usage tracking utilities for DSPy."""

from collections import defaultdict
from collections.abc import Generator
from contextlib import contextmanager
from typing import Any, Generator
from typing import Any

from dspy.dsp.utils.settings import settings

Expand Down
4 changes: 2 additions & 2 deletions tests/primitives/test_base_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ def __call__(self, question: str) -> Prediction:
assert results[0].get_lm_usage() is not None
assert results[1].get_lm_usage() is not None

assert results[0].get_lm_usage().keys() == set(["openai/gpt-4o-mini"])
assert results[1].get_lm_usage().keys() == set(["openai/gpt-3.5-turbo"])
assert results[0].get_lm_usage().keys() == {"openai/gpt-4o-mini"}
assert results[1].get_lm_usage().keys() == {"openai/gpt-3.5-turbo"}


@pytest.mark.asyncio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class ObjectField(BaseModel):


class NestedObjectField(BaseModel):
tupleField: Tuple[str, float]
tupleField: tuple[str, float]
enumField: EnumField
datetimeField: datetime
literalField: LiteralField


class ProgramInputs(BaseModel):
tupleField: Tuple[str, float]
tupleField: tuple[str, float]
enumField: EnumField
datetimeField: datetime
literalField: LiteralField
Expand Down Expand Up @@ -76,15 +76,15 @@ class LiteralField(Enum):


class ProcessedNestedObjectField(BaseModel):
tupleField: Tuple[str, float]
tupleField: tuple[str, float]
enumField: EnumField
datetimeField: datetime
literalField: LiteralField
additionalField: bool


class ProgramOutputs(BaseModel):
processedTupleField: Tuple[str, float]
processedTupleField: tuple[str, float]
processedEnumField: ProcessedEnumField
processedDatetimeField: datetime
processedLiteralField: ProcessedLiteralField
Expand Down
4 changes: 2 additions & 2 deletions tests/reliability/generate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
def generate_test_cases(
dst_path: str,
num_inputs: int = 1,
program_instructions: Optional[str] = None,
input_instructions: Optional[str] = None,
program_instructions: str | None = None,
input_instructions: str | None = None,
) -> list[GeneratedTestCase]:
os.makedirs(dst_path, exist_ok=True)
if _directory_contains_program(dst_path):
Expand Down
18 changes: 9 additions & 9 deletions tests/reliability/generate/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def wrapper(*args, **kwargs):


@_retry(retries=5)
def generate_test_program(dst_path: str, additional_instructions: Optional[str] = None) -> dspy.Module:
def generate_test_program(dst_path: str, additional_instructions: str | None = None) -> dspy.Module:
"""
Generate a DSPy program for a reliability test case and save it to a destination path.

Expand Down Expand Up @@ -81,7 +81,7 @@ def generate_models(schema: dict[str, Any], class_name: str) -> str:
_remove_line_from_file(tmp_model_path, "from __future__ import annotations")
# Remove comments inserted by datamodel-code-generator from the generated model file
_remove_comments_from_file(tmp_model_path)
with open(tmp_model_path, "r") as f:
with open(tmp_model_path) as f:
return f.read()

def rename_conflicting_fields(
Expand Down Expand Up @@ -155,7 +155,7 @@ def generate_test_inputs(
dst_path: str,
program_path: str,
num_inputs: int,
additional_instructions: Optional[str] = None,
additional_instructions: str | None = None,
):
"""
Generate test inputs for a reliability test case and save them to a destination path.
Expand Down Expand Up @@ -226,7 +226,7 @@ def find_max_input_number(directory):
shutil.copytree(tmp_dir, dst_path, dirs_exist_ok=True)


def load_generated_program(path) -> Tuple[dspy.Module, pydantic.BaseModel]:
def load_generated_program(path) -> tuple[dspy.Module, pydantic.BaseModel]:
"""
Loads a generated program from the specified file.

Expand Down Expand Up @@ -283,7 +283,7 @@ def load_generated_cases(dir_path) -> list[GeneratedTestCase]:
# Load each JSON test input file in the inputs directory
for input_file in os.listdir(inputs_path):
if input_file.endswith(".json"):
with open(os.path.join(inputs_path, input_file), "r") as f:
with open(os.path.join(inputs_path, input_file)) as f:
# Best effort to extract a meaningful enclosing directory name
# from the test path that can be used as part of the test case name
readable_dir_name = os.path.basename(os.path.dirname(os.path.dirname(root)))
Expand Down Expand Up @@ -551,7 +551,7 @@ def expand_refs(schema: dict[str, Any], definitions: dict[str, Any]) -> dict[str
return expand_refs(signature_schema_with_refs, definitions)


def _split_schema(schema: dict[str, Any]) -> Tuple[dict[str, Any], dict[str, Any]]:
def _split_schema(schema: dict[str, Any]) -> tuple[dict[str, Any], dict[str, Any]]:
"""
Split a JSON schema into input and output components based on DSPy field types.

Expand Down Expand Up @@ -664,7 +664,7 @@ def _remove_line_from_file(file_path: str, line_to_remove: str):
line_to_remove: The line to remove from the file.
"""
# Read all lines from the file
with open(file_path, "r") as file:
with open(file_path) as file:
lines = file.readlines()

# Write all lines back except the one to remove
Expand All @@ -682,7 +682,7 @@ def _remove_comments_from_file(file_path: str) -> None:
file_path: Path to the file where comments should be removed.
"""
# Read the file contents
with open(file_path, "r") as file:
with open(file_path) as file:
lines = file.readlines()

# Filter out lines that start with '#'
Expand All @@ -693,7 +693,7 @@ def _remove_comments_from_file(file_path: str) -> None:
file.writelines(cleaned_lines)


def _write_pretty_json(data: dict[str, Any], path: Optional[str] = None) -> Optional[str]:
def _write_pretty_json(data: dict[str, Any], path: str | None = None) -> str | None:
"""
Format JSON data with indentation, and write it to a file if specified.

Expand Down
4 changes: 2 additions & 2 deletions tests/reliability/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def assert_program_output_correct(
program_input: Any,
program_output: Any,
grading_guidelines: Union[str, list[str]],
grading_guidelines: str | list[str],
):
"""
With the help of an LLM judge, assert that the specified output of a DSPy program is correct,
Expand Down Expand Up @@ -121,7 +121,7 @@ class ReliabilityTestConf(pydantic.BaseModel):
@lru_cache(maxsize=None)
def parse_reliability_conf_yaml(conf_file_path: str) -> ReliabilityTestConf:
try:
with open(conf_file_path, "r") as file:
with open(conf_file_path) as file:
conf = yaml.safe_load(file)

model_dict = {}
Expand Down
Loading