mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
add check
This commit is contained in:
@@ -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
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user