From 2703df6fdf7e3cb28229533260d13133cba8c749 Mon Sep 17 00:00:00 2001 From: giria660 Date: Thu, 26 Mar 2026 20:21:46 -0400 Subject: [PATCH] Fix Annotated ForwardRef dependencies with future annotations --- fastapi/dependencies/utils.py | 14 ++++- ...gified_annotation_forwardref_dependency.py | 59 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/test_stringified_annotation_forwardref_dependency.py diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index 6b14dac8dc..e01e5d3f83 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -1,6 +1,7 @@ import dataclasses import inspect import sys +from builtins import __dict__ as builtins_dict from collections.abc import ( AsyncGenerator, AsyncIterable, @@ -242,10 +243,21 @@ def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature: return typed_signature +class _LenientTypeResolutionDict(dict[str, Any]): + def __missing__(self, key: str) -> Any: + value = builtins_dict.get(key, ForwardRef(key)) + self[key] = value + return value + + def get_typed_annotation(annotation: Any, globalns: dict[str, Any]) -> Any: if isinstance(annotation, str): annotation = ForwardRef(annotation) - annotation = evaluate_forwardref(annotation, globalns, globalns) # ty: ignore[deprecated] + if isinstance(annotation, ForwardRef): + lenient_globalns = _LenientTypeResolutionDict(globalns) + annotation = evaluate_forwardref( # ty: ignore[deprecated] + annotation, lenient_globalns, lenient_globalns + ) if annotation is type(None): return None return annotation diff --git a/tests/test_stringified_annotation_forwardref_dependency.py b/tests/test_stringified_annotation_forwardref_dependency.py new file mode 100644 index 0000000000..1778ad7a18 --- /dev/null +++ b/tests/test_stringified_annotation_forwardref_dependency.py @@ -0,0 +1,59 @@ +from __future__ import annotations + +from dataclasses import dataclass +from typing import Annotated + +from fastapi import Depends, FastAPI +from fastapi.testclient import TestClient +from inline_snapshot import snapshot + +app = FastAPI() + + +def get_potato() -> Potato: + return Potato(color="red", size=10) + + +@app.get("/") +async def read_root(potato: Annotated[Potato, Depends(get_potato)]): + return {"color": potato.color, "size": potato.size} + + +@dataclass +class Potato: + color: str + size: int + + +client = TestClient(app) + + +def test_stringified_annotated_forwardref_dependency(): + response = client.get("/") + assert response.status_code == 200, response.text + assert response.json() == {"color": "red", "size": 10} + + +def test_stringified_annotated_forwardref_dependency_openapi(): + response = client.get("/openapi.json") + assert response.status_code == 200, response.text + assert response.json() == snapshot( + { + "openapi": "3.1.0", + "info": {"title": "FastAPI", "version": "0.1.0"}, + "paths": { + "/": { + "get": { + "summary": "Read Root", + "operationId": "read_root__get", + "responses": { + "200": { + "description": "Successful Response", + "content": {"application/json": {"schema": {}}}, + } + }, + } + } + }, + } + )