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)
|
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)
|
@router.post("/predict", status_code=status.HTTP_200_OK, response_model=Trace)
|
||||||
async def predict(input_value: schema) -> Trace: # type: ignore
|
async def predict(input_value: schema) -> Trace: # type: ignore
|
||||||
|
|
@ -25,9 +30,12 @@ def bootstrap_prediction_endpoint(
|
||||||
arguments = {
|
arguments = {
|
||||||
name: getattr(input_value, name) for name in schema.model_fields
|
name: getattr(input_value, name) for name in schema.model_fields
|
||||||
}
|
}
|
||||||
|
positional = [arguments.pop(name) for name in positional_names]
|
||||||
if inspect.iscoroutinefunction(func):
|
if inspect.iscoroutinefunction(func):
|
||||||
return await cast(Callable[..., Awaitable[Trace]], func)(**arguments)
|
return await cast(Callable[..., Awaitable[Trace]], func)(
|
||||||
return cast(Callable[..., Trace], func)(**arguments)
|
*positional, **arguments
|
||||||
|
)
|
||||||
|
return cast(Callable[..., Trace], func)(*positional, **arguments)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
|
|
||||||
|
|
@ -9,16 +9,7 @@ def get_arguments(
|
||||||
|
|
||||||
signature = inspect.signature(func)
|
signature = inspect.signature(func)
|
||||||
|
|
||||||
defaults = {
|
# Model-injected parameters may not have been supplied at this wrapper layer.
|
||||||
p.name: p.default
|
arguments = signature.bind_partial(*args, **kwargs)
|
||||||
for p in signature.parameters.values()
|
arguments.apply_defaults()
|
||||||
if p.default != inspect._empty
|
return dict(arguments.arguments)
|
||||||
}
|
|
||||||
|
|
||||||
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}
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,8 @@ def automatically_decorate_parameters(func: F) -> F:
|
||||||
parameter_names = [
|
parameter_names = [
|
||||||
param.name
|
param.name
|
||||||
for param in signature.parameters.values()
|
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)
|
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:
|
with TestClient(predict.app) as client:
|
||||||
assert client.post("/predict", json={"value": 8}).json()["output"] == 8
|
assert client.post("/predict", json={"value": 8}).json()["output"] == 8
|
||||||
assert client.post("/predict", json={"value": "wrong"}).status_code == 422
|
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