Fix Annotated with ForwardRef when using future annotations by majiayu000 · Pull Request #14557 · fastapi/fastapi

and others added 7 commits

December 18, 2025 00:27
When using `from __future__ import annotations` with `Annotated[SomeClass, Depends()]`
where SomeClass is defined after the function, FastAPI was unable to properly
resolve the forward reference and treated the parameter as a query parameter
instead of a dependency.

This fix adds special handling to partially resolve Annotated string annotations,
extracting metadata (like Depends) even when the inner type cannot be fully resolved.

Fixes fastapi#13056
- Remove unused type: ignore comments that caused mypy lint failure
- Only apply the partial Annotated resolution for Depends metadata
- For other metadata types (File, Form, Query, etc.), return None to
  preserve the original type detection logic
- This fixes the test failures where File/UploadFile params were being
  incorrectly identified as query parameters

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Fix Python 3.8 compatibility by importing Annotated from typing_extensions
  when running on Python < 3.9
- Simplify get_typed_signature to not use get_type_hints which was causing
  issues with Pydantic v1 parameter detection (Header, Cookie, Query, etc.)
- Instead, expand globalns with module namespace to help resolve forward
  references while keeping the original annotation resolution logic intact

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

@majiayu000

Add unit tests for:
- Invalid annotation formats (missing prefix/suffix)
- Non-Depends metadata (Query, etc.)
- Eval failures for undefined functions
- Nested brackets in type annotations
- ForwardRef type resolution failures
- Exception handling with partial resolution fallback
- Resolved Annotated types
- get_typed_signature edge cases

These tests improve coverage for the forward reference handling code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

@pre-commit-ci-lite

The Depends function returns a params.Depends instance, so we need to
check isinstance against params.Depends, not the Depends function itself.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>