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.

Revisions

  1. rpgoldman created this gist May 3, 2024.
    3 changes: 3 additions & 0 deletions mypy-output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    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)
    40 changes: 40 additions & 0 deletions mypy_issue.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    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