Browse Source

fix(dependencies): resolve forward ref in Annotated type_annotation

When using 'from __future__ import annotations', the type inside Annotated
remains as a string (forward reference). The fix resolves it using
get_typed_annotation which calls evaluate_forwardref.

Fixes: Can't use Annotated with ForwardRef (#13056)
pull/15338/head
RoomWithOutRoof 3 months ago
parent
commit
a8d8801f53
  1. 5
      fastapi/dependencies/utils.py

5
fastapi/dependencies/utils.py

@ -411,6 +411,11 @@ def analyze_param(
if get_origin(use_annotation) is Annotated:
annotated_args = get_args(annotation)
type_annotation = annotated_args[0]
# Resolve forward reference in type_annotation
if isinstance(type_annotation, str):
# Try to resolve using endpoint's globalns
globalns = getattr(call, "__globals__", {})
type_annotation = get_typed_annotation(type_annotation, globalns)
fastapi_annotations = [
arg
for arg in annotated_args[1:]

Loading…
Cancel
Save