Fix Annotated with ForwardRef when using future annotations by AhsanSheraz · Pull Request #15096 · fastapi/fastapi

@ahsansheraz-bonial

…tations`

When `from __future__ import annotations` is active, annotations become
strings.  If a type referenced inside `Annotated[T, Depends(...)]` is
defined after the route decorator, `T` is not yet available at decoration
time and the full `Annotated` string cannot be resolved.  FastAPI then
loses the `Depends` metadata and incorrectly treats the parameter as a
query parameter.

The fix introduces a lenient evaluation strategy: when strict evaluation
of the annotation string fails, a `_LenientDict` namespace is used where
undefined names resolve to `Any`.  This preserves the `Annotated`
structure and allows FastAPI to extract `Depends` metadata.  The lenient
result is only accepted when it contains `Depends` metadata — for other
metadata types (Query, Body, etc.) the actual type is needed, so the
existing fallback behaviour is kept.

Fixes fastapi#13056

Made-with: Cursor