From bc2c6f9fa69b59101ad34d5df4c19583d2df04b5 Mon Sep 17 00:00:00 2001 From: Jason Liu Date: Sat, 14 Oct 2023 16:04:10 -0400 Subject: [PATCH] add check --- instructor/distil.py | 3 +++ tests/test_distil.py | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/instructor/distil.py b/instructor/distil.py index c69cdb3..179a9fb 100644 --- a/instructor/distil.py +++ b/instructor/distil.py @@ -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) diff --git a/tests/test_distil.py b/tests/test_distil.py index beed033..474cf1d 100644 --- a/tests/test_distil.py +++ b/tests/test_distil.py @@ -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)