fastapi-cache icon indicating copy to clipboard operation
fastapi-cache copied to clipboard

It reports error with "got multiple values for argument " when I use the request object directly

Open fangd123 opened this issue 2 years ago • 1 comments

My code is as follows:

@app.post('/oneline', response_model=ClassifyResult)
@cache()
async def predict(sentences: Sentences,request:Request):
    texts = sentences.texts
    type = sentences.type

It reports error

File "C:\Users\fang_\anaconda3\envs\hmqf_dl\lib\site-packages\fastapi_cache\decorator.py", line 52, in inner
TypeError: predict() got multiple values for argument 'sentences'

after debug the code, I found that the reson maybe these line in decorator.py:

if request.method != "GET":
    return await func(request, *args, **kwargs)

The **kwargs has the arguments request, so it has multiple request

after I modify the code

if request.method != "GET":
    return await func(*args, **kwargs)

it runs ok, but I don't know whether it has other influence.

So was it a bug or I use it a wrong way?

fangd123 avatar Oct 21 '21 06:10 fangd123

IMO this is a bug. Since that decorator uses pop in order to extract the request from kwargs, but the issue is that it really uses a copy of kwargs, then when calls with func(request, *args, **kwargs) use the original kwargs with the request key in it.

lucholeves avatar Aug 03 '22 18:08 lucholeves

This has been addressed by #130.

mjpieters avatar May 14 '23 21:05 mjpieters