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: handle __call__ on clients #4618

Merged
merged 2 commits into from
Apr 8, 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
8 changes: 8 additions & 0 deletions src/_bentoml_impl/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class AbstractClient(abc.ABC):

def __init__(self) -> None:
for name in self.endpoints:
if name == "__call__":
# __call__ must be set on the class
continue
attr_name = name
if getattr(self, attr_name, None) is not None:
attr_name = f"api_{name}" # prefix to avoid name conflict
Expand All @@ -49,3 +52,8 @@ def call(self, __name: str, /, *args: t.Any, **kwargs: t.Any) -> t.Any:
"""Call a service method by its name.
It takes the same arguments as the service method.
"""

def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Any:
if "__call__" not in self.endpoints:
raise TypeError("This service is not callable.")
return self.call("__call__", *args, **kwargs)
13 changes: 10 additions & 3 deletions src/_bentoml_sdk/service/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,21 @@ def __init__(self) -> None:

class _AsyncWrapper:
def __init__(self, wrapped: t.Any, apis: t.Iterable[str]) -> None:
self.__call = None
for name in apis:
setattr(self, name, self.__make_method(wrapped, name))
if name == "__call__":
self.__call = self.__make_method(wrapped, name)
else:
setattr(self, name, self.__make_method(wrapped, name))

def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Any:
if self.__call is None:
raise TypeError("This service is not callable.")
return self.__call(*args, **kwargs)

def __make_method(self, inner: t.Any, name: str) -> t.Any:
import asyncio
import subprocess

subprocess.Popen
import anyio.to_thread

original_func = func = getattr(inner, name)
Expand Down
Loading