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: make inference_api handle None type #3611

Merged
merged 6 commits into from
Mar 2, 2023
Merged
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
34 changes: 23 additions & 11 deletions src/bentoml/_internal/service/inference_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,32 @@
class InferenceAPI:
def __init__(
self,
user_defined_callback: t.Callable[..., t.Any],
user_defined_callback: t.Callable[..., t.Any] | None,
input_descriptor: IODescriptor[t.Any],
output_descriptor: IODescriptor[t.Any],
name: Optional[str],
doc: Optional[str] = None,
route: Optional[str] = None,
):
# Use user_defined_callback function variable if name not specified
name = user_defined_callback.__name__ if name is None else name
# Use user_defined_callback function docstring `__doc__` if doc not specified
doc = user_defined_callback.__doc__ if doc is None else doc
if user_defined_callback is not None:
# Use user_defined_callback function variable if name not specified
name = user_defined_callback.__name__ if name is None else name
# Use user_defined_callback function docstring `__doc__` if doc not specified
doc = user_defined_callback.__doc__ if doc is None else doc
else:
name, doc = "", ""

# Use API name as route if route not specified
route = name if route is None else route

InferenceAPI._validate_name(name)
InferenceAPI._validate_route(route)

self.name = name
self.needs_ctx = False
self.ctx_param = None
self.func = user_defined_callback
input_type = input_descriptor.input_type()
self.multi_input = isinstance(input_type, dict)

if user_defined_callback is not None:
InferenceAPI._validate_name(name)
InferenceAPI._validate_route(route)

sig = inspect.signature(user_defined_callback)

if len(sig.parameters) == 0:
Expand Down Expand Up @@ -126,6 +127,17 @@ def __init__(
f"Expected type of argument '{second_arg}' to be '{input_type}', got '{sig.parameters[second_arg].annotation}'"
)

if user_defined_callback is not None:
self.func = user_defined_callback
else:

def nop(*args: t.Any, **kwargs: t.Any):
return

self.func = nop
aarnphm marked this conversation as resolved.
Show resolved Hide resolved

self.name = name
self.multi_input = isinstance(input_type, dict)
self.input = input_descriptor
self.output = output_descriptor
self.doc = doc
Expand Down