Fix Annotated ForwardRef dependencies with postponed annotations#5
Fix Annotated ForwardRef dependencies with postponed annotations#5yashwant86 wants to merge 5 commits intomasterfrom
Conversation
Changes
Sequence DiagramsequenceDiagram
participant Client
participant FastAPI
participant DepUtils as DependencyUtils
participant LenientDict as _LenientTypeResolutionDict
Client->>FastAPI: GET /
FastAPI->>DepUtils: get_typed_annotation("Annotated[Potato, Depends(get_potato)]", globalns)
DepUtils->>DepUtils: Convert string to ForwardRef
DepUtils->>DepUtils: evaluate_forwardref(ForwardRef, globalns, globalns)
alt ForwardRef resolved
DepUtils-->>FastAPI: Return resolved annotation
else ForwardRef still unresolved
DepUtils->>LenientDict: Create with globalns
DepUtils->>DepUtils: eval(forward_arg, LenientDict)
LenientDict->>LenientDict: __missing__("Potato") → ForwardRef("Potato")
DepUtils-->>FastAPI: Return Annotated[ForwardRef("Potato"), Depends(...)]
end
FastAPI->>FastAPI: Resolve dependency get_potato()
FastAPI-->>Client: {"color": "red", "size": 10}
Dig Deeper With Commands
Runs only when explicitly triggered. |
|
/roast fastapi/dependencies/utils.py |
/roast fastapi/dependencies/utils.pyEval-ing user ForwardRefs with builtins fallback and bare |
Actionable Comments Posted: 0🧹 Nitpick comments (1)Broad \`except Exception: pass\` silently swallows all eval failures - fastapi/dependencies/utils.py (258, 263)🧾 Coverage Summary✔️ Covered (3 files) |
Actionable Comments Posted: 0👀 Worth a Look (1)[MEDIUM] Bare `except Exception: pass` silently swallows eval failures - fastapi/dependencies/utils.py (262, 263)🧾 Coverage Summary✔️ Covered (3 files) |
|
/review --force |
| annotation = eval( # noqa: S307 | ||
| annotation.__forward_arg__, _LenientTypeResolutionDict(globalns) | ||
| ) | ||
| except Exception: |
There was a problem hiding this comment.
Bare except Exception: pass silently swallows all eval errors
The fallback eval() catches and discards all exceptions including SyntaxError, NameError, and TypeError. When a developer has a genuine typo or syntax error in a complex Annotated type annotation, this will silently degrade to an unresolved ForwardRef with no diagnostic output, making the root cause very hard to track down. Consider logging a debug/warning message or narrowing the exception type.
Add a logger.debug() or warnings.warn() inside the except block so developers can diagnose annotation resolution failures, e.g. except Exception as e: logger.debug(f'Failed to resolve ForwardRef {annotation.__forward_arg__!r}: {e}').
Actionable Comments Posted: 1🧾 Coverage Summary✔️ Covered (3 files) |
Mirror of fastapi#15234
Summary by MergeMonkey