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 ourterms of serviceand privacy statement.We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot useResponsetype hint as dependency annotation #10127

Open
1 task
Kludexopened this issue Aug 22, 2023 · 4 comments
Open
1 task

Cannot useResponsetype hint as dependency annotation #10127

Kludexopened this issue Aug 22, 2023 · 4 comments

Comments

@Kludex
Copy link
Member

Kludex commented Aug 22, 2023

Privileged issue

  • I'm@tiangoloor he asked me directly to create an issue here.

Issue Content

FastAPI0.95.0introducedAnnotatedas a way to create dependencies (https://fastapi.tiangolo /release-notes/#0950).

Before0.95.0,it was possible to useResponseas a type hint for dependencies. After that, it was not possible anymore.

Example that worked before:

fromtypingimportAnnotated
fromfastapiimportFastAPI,Response,Depends

app=FastAPI()

defset_no_cache_headers(response:Response)->Response:
response.headers["Potato"]="potato"
returnresponse

@app.get("/")
deftesting_response(response:Response=Depends(set_no_cache_headers)):
return{}

As a workaround today, if you remove theResponsetype hint, it works as expected:

@app.get("/")
deftesting_response(response=Depends(set_no_cache_headers)):
return{}

TheAnnotatedway also doesn't work:

@app.get("/")
deftesting_response(response:Annotated[Response,Depends(set_no_cache_headers)]):
return{}
@iudeen
Copy link
Contributor

iudeen commented Aug 22, 2023

Given how obvious the block is in code, should we assume that it was intentional and update the docs?

@AhsanSheraz

This comment was marked as spam.

@ahmedabdou14
Copy link
Contributor

Hi, any update on this issue?

There is a similar issue to this one aswell

Given a response defined in the api parameters

This code produces aError: response status is 502

@router.get("/test")
deftest(res:Response):
# res.status_code = 200
returnres

last error log

File"/usr/local/lib/ Python 3.12/site-packages/uvicorn/protocols/http/h11_impl.py",line 488,insend
reason = STATUS_PHRASES[status]

However if we change anything in the response

It works

@router.get("/test")
deftest(res:Response):
res.status_code=200
returnres

This is very weird because the default value of theResponse.status_codeis200anyway.
I was not able to understand how exactly can aResponsebe put in the endpoint parameter list withoutDependsas it will not be explicitly initialized.

The couple of errors presented by@Kludex& the one I reported above means maybe we need a big refactor on how Response class works? It has been over a year on this issue; I wonder why it has been up all this time ^^. Thank you

@ShreySinha02
Copy link

ShreySinha02 commented Feb 10, 2024

Hii,
is this issue is related to this part of code can you please guide me on this

responses:Annotated[
Optional[Dict[Union[int,str],Dict[str,Any]]],
Doc(
"""
Additional responses that could be returned by this *path operation*.

It will be added to the generated OpenAPI (e.g. visible at `/docs`).
"""
),
]=None,


whysage pushed a commit to whysage/fastapi that referenced this issue Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants