Skip to content

Commit

Permalink
fix: make inference_api handle None type (#3611)
Browse files Browse the repository at this point in the history
Co-authored-by: Sauyon Lee <[email protected]>
  • Loading branch information
aarnphm and sauyon authored Mar 2, 2023
1 parent 368a7a9 commit 699c9c2
Showing 1 changed file with 23 additions and 11 deletions.
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

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

0 comments on commit 699c9c2

Please sign in to comment.