Skip to content

Fix style errors of dspy/signatures #8186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2025
Merged
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: 3 additions & 4 deletions dspy/signatures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from dspy.signatures.field import InputField, OutputField, OldField, OldInputField, OldOutputField
from dspy.signatures.field import InputField, OldField, OldInputField, OldOutputField, OutputField
from dspy.signatures.signature import (
SignatureMeta,
Signature,
SignatureMeta,
ensure_signature,
make_signature,
infer_prefix,
make_signature,
)


__all__ = [
"InputField",
"OutputField",
Expand Down
4 changes: 2 additions & 2 deletions dspy/signatures/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def _translate_pydantic_field_constraints(**kwargs):
return ", ".join(constraints)


def InputField(**kwargs):
def InputField(**kwargs): # noqa: N802
return pydantic.Field(**move_kwargs(**kwargs, __dspy_field_type="input"))


def OutputField(**kwargs):
def OutputField(**kwargs): # noqa: N802
return pydantic.Field(**move_kwargs(**kwargs, __dspy_field_type="output"))


Expand Down
14 changes: 7 additions & 7 deletions dspy/signatures/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MySignature(dspy.Signature):
import types
import typing
from copy import deepcopy
from typing import Any, Dict, Tuple, Type, Union # noqa: UP035
from typing import Any, Dict, Optional, Tuple, Type, Union

from pydantic import BaseModel, Field, create_model
from pydantic.fields import FieldInfo
Expand All @@ -38,7 +38,7 @@ def _default_instructions(cls) -> str:


class SignatureMeta(type(BaseModel)):
def __call__(cls, *args, **kwargs): # noqa: ANN002
def __call__(cls, *args, **kwargs):
if cls is Signature:
# We don't create an actual Signature instance, instead, we create a new Signature class.
return make_signature(*args, **kwargs)
Expand Down Expand Up @@ -105,7 +105,7 @@ def instructions(cls) -> str:

@instructions.setter
def instructions(cls, instructions: str) -> None:
setattr(cls, "__doc__", instructions)
cls.__doc__ = instructions

@property
def input_fields(cls) -> dict[str, FieldInfo]:
Expand Down Expand Up @@ -144,11 +144,11 @@ def __repr__(cls):
for name, field in cls.fields.items():
field_reprs.append(f"{name} = Field({field})")
field_repr = "\n ".join(field_reprs)
return f"{cls.__name__}({cls.signature}\n instructions={repr(cls.instructions)}\n {field_repr}\n)"
return f"{cls.__name__}({cls.signature}\n instructions={cls.instructions!r}\n {field_repr}\n)"


class Signature(BaseModel, metaclass=SignatureMeta):
"" # noqa: D419
""

# Note: Don't put a docstring here, as it will become the default instructions
# for any signature that doesn't define it's own instructions.
Expand Down Expand Up @@ -203,7 +203,7 @@ def delete(cls, name) -> Type["Signature"]:
return Signature(fields, cls.instructions)

@classmethod
def insert(cls, index: int, name: str, field, type_: Type = None) -> Type["Signature"]:
def insert(cls, index: int, name: str, field, type_: Optional[Type] = None) -> Type["Signature"]:
# It's possible to set the type as annotation=type in pydantic.Field(...)
# But this may be annoying for users, so we allow them to pass the type
if type_ is None:
Expand Down Expand Up @@ -280,7 +280,7 @@ def ensure_signature(signature: Union[str, Type[Signature]], instructions=None)

def make_signature(
signature: Union[str, Dict[str, Tuple[type, FieldInfo]]],
instructions: str = None,
instructions: Optional[str] = None,
signature_name: str = "StringSignature",
) -> Type[Signature]:
"""Create a new Signature subclass with the specified fields and instructions.
Expand Down