Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KeyError in _validate_and_extract_signature_types and Add Tests for Pydantic Actions #403

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions burr/integrations/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,13 @@ def _validate_and_extract_signature_types(
)
type_hints = typing.get_type_hints(fn)

if (state_model := type_hints["state"]) is inspect.Parameter.empty or not issubclass(
state_model, pydantic.BaseModel
):
state_model = type_hints.get("state")
if state_model is None or state_model is inspect.Parameter.empty or not issubclass(state_model, pydantic.BaseModel):
raise ValueError(
f"Function fn: {fn.__qualname__} is not a valid pydantic action. "
"a type annotation of a type extending: pydantic.BaseModel. Got parameter "
"state: {state_model.__qualname__}."
"The 'state' parameter must be annotated with a type extending pydantic.BaseModel."
)

if (ret_hint := type_hints.get("return")) is None or not issubclass(
ret_hint, pydantic.BaseModel
):
Expand Down
12 changes: 6 additions & 6 deletions tests/integrations/test_burr_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,27 +154,27 @@ def test_model_from_state():


def _fn_without_state_arg(foo: OriginalModel) -> OriginalModel:
...
return foo;


def _fn_with_incorrect_state_arg(state: int) -> OriginalModel:
...
return OriginalModel(foo, bar)


def _fn_with_incorrect_return_type(state: OriginalModel) -> int:
...
return 42


def _fn_with_no_return_type(state: OriginalModel):
...
pass


def _fn_correct_same_itype_otype(state: OriginalModel, input_1: int) -> OriginalModel:
...
return state


def _fn_correct_diff_itype_otype(state: OriginalModel, input_1: int) -> NestedModel:
...
return NestedModel(nested_field1=input_1)


@pytest.mark.parametrize(
Expand Down