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

chore(io): allow implicit pydantic type conversion #4534

Merged
merged 4 commits into from
Feb 28, 2024
Merged
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
16 changes: 14 additions & 2 deletions src/_bentoml_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import typing as t

import attrs
import pydantic

from bentoml._internal.service.openapi import SUCCESS_DESCRIPTION
from bentoml._internal.service.openapi.specification import MediaType
from bentoml._internal.service.openapi.specification import Schema
from bentoml._internal.utils import dict_filter_none

from .io_models import IODescriptor
from .io_models import ensure_io_descriptor

R = t.TypeVar("R")
T = t.TypeVar("T", bound="APIMethod[..., t.Any]")
Expand All @@ -28,13 +30,23 @@ def _only_include(data: dict[str, t.Any], fields: t.Container[str]) -> dict[str,
return {k: v for k, v in data.items() if k in fields}


def _io_descriptor_converter(it: t.Any) -> type[IODescriptor]:
if not inspect.isclass(it):
raise ValueError(f"{it} must be a class type")
if not issubclass(it, (IODescriptor, pydantic.BaseModel)):
raise ValueError(f"{it} is not a valid IODescriptor accepted type.")
if issubclass(it, IODescriptor):
return it
return ensure_io_descriptor(it)
aarnphm marked this conversation as resolved.
Show resolved Hide resolved


@attrs.define
class APIMethod(t.Generic[P, R]):
func: t.Callable[t.Concatenate[t.Any, P], R]
route: str = attrs.field()
name: str = attrs.field()
input_spec: type[IODescriptor] = attrs.field()
output_spec: type[IODescriptor] = attrs.field()
input_spec: type[IODescriptor] = attrs.field(converter=_io_descriptor_converter)
output_spec: type[IODescriptor] = attrs.field(converter=_io_descriptor_converter)
batchable: bool = False
batch_dim: tuple[int, int] = attrs.field(
default=(0, 0), converter=lambda x: (x, x) if not isinstance(x, tuple) else x
Expand Down
18 changes: 11 additions & 7 deletions src/_bentoml_sdk/io_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,18 @@ def from_output(cls, func: t.Callable[..., t.Any]) -> type[IODescriptor]:
def ensure_io_descriptor(output_type: type) -> type[IODescriptor]:
if inspect.isclass(output_type) and issubclass(output_type, BaseModel):
if not issubclass(output_type, IODescriptor):

class Output(IOMixin, output_type):
pass

return t.cast(t.Type[IODescriptor], Output)

return t.cast(
t.Type[IODescriptor],
create_model(
f"{output_type.__name__}IODescriptor",
__base__=(IOMixin, output_type),
),
)
return output_type
return t.cast(
t.Type[IODescriptor],
create_model("Output", __base__=(IOMixin, RootModel[output_type])), # type: ignore
create_model(
f"{output_type.__name__}IODescriptor",
__base__=(IOMixin, RootModel[output_type]),
),
)
Loading