add check

This commit is contained in:
Jason Liu
2023-10-14 16:04:10 -04:00
parent 2bb47034c2
commit bc2c6f9fa6
2 changed files with 20 additions and 1 deletions
+3
View File
@@ -69,6 +69,9 @@ def is_return_type_base_model_or_instance(func: Callable[..., Any]) -> bool:
:return: True if the return type is a pydantic BaseModel or an instance of it.
"""
return_type = inspect.signature(func).return_annotation
assert (
return_type != inspect.Signature.empty
), "Must have a return type hint that is a pydantic BaseModel"
return inspect.isclass(return_type) and issubclass(return_type, BaseModel)
+17 -1
View File
@@ -1,4 +1,4 @@
from pyexpat import model
import pytest
import openai
from pydantic import BaseModel
from instructor.distil import (
@@ -19,6 +19,22 @@ class SimpleModel(BaseModel):
data: int
def test_must_have_hint():
with pytest.raises(AssertionError):
@instructions.distil
def test_func(x: int):
return SimpleModel(data=x)
def test_must_be_base_model():
with pytest.raises(AssertionError):
@instructions.distil
def test_func(x) -> int:
return SimpleModel(data=x)
def test_is_return_type_base_model_or_instance():
def valid_function() -> SimpleModel:
return SimpleModel(data=1)