Skip to content

Instantly share code, notes, and snippets.

@rpgoldman
Created May 3, 2024 20:53
Show Gist options
  • Save rpgoldman/44ab1c8352f0f8bf985d6ec29a83db20 to your computer and use it in GitHub Desktop.
Save rpgoldman/44ab1c8352f0f8bf985d6ec29a83db20 to your computer and use it in GitHub Desktop.
Issue with Pydantic Models
dspy/signatures/mypy_issue.py:35: error: Too many arguments for "Signature" [call-arg]
dspy/signatures/mypy_issue.py:35: error: Incompatible return value type (got "Signature", expected "type[Signature]") [return-value]
Found 2 errors in 1 file (checked 1 source file)
import typing
from typing import Union, Type, Optional
import pydantic.fields
from pydantic import BaseModel, Field, create_model
from pydantic.fields import FieldInfo
import dsp
from dspy.signatures.field import InputField, OutputField, new_to_old_field
# I don't love this, but mypy cannot deal with the dynamic lookup of this metaclass - rpg
PydanticMetaClass: typing.TypeAlias = pydantic._internal._model_construction.ModelMetaclass
assert PydanticMetaClass == type(pydantic.BaseModel), \
"Implementation of Signature MetaClass depends on the internals of Pydantic, which have changed."
class SignatureMeta(PydanticMetaClass):
pass
class Signature(BaseModel, metaclass=SignatureMeta):
"""""" # noqa: D419
pass
# example usages
def ensure_signature(
signature: Union[str, Type[Signature]], instructions: Optional[str] = None
) -> Type[Signature]:
if signature is None:
raise ValueError("Signature argument is mandatory")
if isinstance(signature, str):
# FIXME: According to mypy, Signature cannot be called with 2 arguments.
return Signature(signature, instructions)
if instructions is not None:
raise ValueError(
"Don't specify instructions when initializing with a Signature"
)
return signature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment