Request type hint should work in dependency chain.
()
| 123 | |
| 124 | # Tests for Request type hint with Depends |
| 125 | def test_request_with_depends_annotated(): |
| 126 | """Request type hint should work in dependency chain.""" |
| 127 | app = FastAPI() |
| 128 | |
| 129 | def extract_request_info(request: Request) -> dict: |
| 130 | return { |
| 131 | "path": request.url.path, |
| 132 | "user_agent": request.headers.get("user-agent", "unknown"), |
| 133 | } |
| 134 | |
| 135 | @app.get("/") |
| 136 | def endpoint( |
| 137 | info: Annotated[dict, Depends(extract_request_info)], |
| 138 | ): |
| 139 | return info |
| 140 | |
| 141 | client = TestClient(app) |
| 142 | resp = client.get("/", headers={"user-agent": "test-agent"}) |
| 143 | |
| 144 | assert resp.status_code == 200 |
| 145 | assert resp.json() == {"path": "/", "user_agent": "test-agent"} |
| 146 | |
| 147 | |
| 148 | # Tests for BackgroundTasks type hint with Depends |