Skip to content

Cannot use Response type hint as dependency annotation #10127

@Kludex

Description

@Kludex

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 {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions