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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use Response type hint as dependency annotation #10127

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

Cannot use Response type hint as dependency annotation #10127

Kludex opened this issue Aug 22, 2023 · 4 comments

Comments

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Aug 22, 2023

Privileged issue

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

Issue Content

FastAPI 0.95.0 introduced Annotated as a way to create dependencies (https://fastapi.tiangolo.com/release-notes/#0950).

Before 0.95.0, it was possible to use Response as a type hint for dependencies. After that, it was not possible anymore.

Example that worked before:

from typing import Annotated
from fastapi import FastAPI, Response, Depends

app = FastAPI()

def set_no_cache_headers(response: Response) -> Response:
    response.headers["Potato"] = "potato"
    return response

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

As a workaround today, if you remove the Response type hint, it works as expected:

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

The Annotated way also doesn't work:

@app.get("/")
def testing_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 a Error: response status is 502

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

last error log

File "/usr/local/lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py", line 488, in send
reason = STATUS_PHRASES[status]

However if we change anything in the response

It works

@router.get("/test")
def test(res: Response):
    res.status_code = 200
    return res

This is very weird because the default value of the Response.status_code is 200 anyway.
I was not able to understand how exactly can a Response be put in the endpoint parameter list without Depends as 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