Bind positional-only arguments correctly in direct and HTTP calls
This commit is contained in:
parent
42940fd84c
commit
691140f9f8
4 changed files with 31 additions and 16 deletions
|
|
@ -16,6 +16,11 @@ def bootstrap_prediction_endpoint(
|
|||
)
|
||||
|
||||
schema = _get_schema(func)
|
||||
positional_names = [
|
||||
p.name
|
||||
for p in inspect.signature(func).parameters.values()
|
||||
if p.kind == p.POSITIONAL_ONLY and p.name in schema.model_fields
|
||||
]
|
||||
|
||||
@router.post("/predict", status_code=status.HTTP_200_OK, response_model=Trace)
|
||||
async def predict(input_value: schema) -> Trace: # type: ignore
|
||||
|
|
@ -25,9 +30,12 @@ def bootstrap_prediction_endpoint(
|
|||
arguments = {
|
||||
name: getattr(input_value, name) for name in schema.model_fields
|
||||
}
|
||||
positional = [arguments.pop(name) for name in positional_names]
|
||||
if inspect.iscoroutinefunction(func):
|
||||
return await cast(Callable[..., Awaitable[Trace]], func)(**arguments)
|
||||
return cast(Callable[..., Trace], func)(**arguments)
|
||||
return await cast(Callable[..., Awaitable[Trace]], func)(
|
||||
*positional, **arguments
|
||||
)
|
||||
return cast(Callable[..., Trace], func)(*positional, **arguments)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
|
|
|
|||
|
|
@ -9,16 +9,7 @@ def get_arguments(
|
|||
|
||||
signature = inspect.signature(func)
|
||||
|
||||
defaults = {
|
||||
p.name: p.default
|
||||
for p in signature.parameters.values()
|
||||
if p.default != inspect._empty
|
||||
}
|
||||
|
||||
filter_keys = [
|
||||
param.name
|
||||
for param in signature.parameters.values()
|
||||
if param.kind == param.POSITIONAL_OR_KEYWORD
|
||||
]
|
||||
|
||||
return {**defaults, **dict(zip(filter_keys, args)), **kwargs}
|
||||
# Model-injected parameters may not have been supplied at this wrapper layer.
|
||||
arguments = signature.bind_partial(*args, **kwargs)
|
||||
arguments.apply_defaults()
|
||||
return dict(arguments.arguments)
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@ def automatically_decorate_parameters(func: F) -> F:
|
|||
parameter_names = [
|
||||
param.name
|
||||
for param in signature.parameters.values()
|
||||
if param.kind in (param.POSITIONAL_OR_KEYWORD, param.KEYWORD_ONLY)
|
||||
if param.kind
|
||||
in (param.POSITIONAL_ONLY, param.POSITIONAL_OR_KEYWORD, param.KEYWORD_ONLY)
|
||||
]
|
||||
|
||||
metadata = get_function_metadata_store(func)
|
||||
|
|
|
|||
|
|
@ -17,3 +17,18 @@ def test_keyword_only_inputs_are_validated_and_exposed(isolated_context: None) -
|
|||
with TestClient(predict.app) as client:
|
||||
assert client.post("/predict", json={"value": 8}).json()["output"] == 8
|
||||
assert client.post("/predict", json={"value": "wrong"}).status_code == 422
|
||||
|
||||
|
||||
def test_positional_only_inputs_keep_their_bindings(isolated_context: None) -> None:
|
||||
@GreatAI.create
|
||||
def predict(first: str, /, second: int = 7) -> str:
|
||||
return first * second
|
||||
|
||||
trace = predict("a", 2)
|
||||
assert trace.output == "aa"
|
||||
assert trace.logged_values["arg:first:value"] == "a"
|
||||
assert trace.logged_values["arg:second:value"] == 2
|
||||
with TestClient(predict.app) as client:
|
||||
response = client.post("/predict", json={"first": "b", "second": 3})
|
||||
assert response.status_code == 200
|
||||
assert response.json()["output"] == "bbb"
|
||||
|
|
|
|||
Loading…
Reference in a new issue