ruff
df7e8268 - [`fastapi`] Handle callable class dependencies with `__call__` method (`FAST003`) (#23553)

Commit
185 days ago
[`fastapi`] Handle callable class dependencies with `__call__` method (`FAST003`) (#23553) Fixes #23526 ## Problem When a class with a `__call__` method (but no `__init__`) is used as a FastAPI dependency, FAST003 emits a false positive: ```python class Query: def __call__(self, thing_id: int): pass @app.get("/things/{thing_id}") async def read_thing(query: Annotated[str, Depends(Query)]): ... # FAST003: Parameter `thing_id` appears in route path, but not in `read_thing` signature ``` ## Root Cause In `from_dependency_name`, the `ClassDefinition` branch checked for Pydantic base models and `__init__`, but returned `None` (not `Some(Self::Unknown)`) when neither was found. This caused the dependency to be silently skipped in the caller, leaving the path parameter unmatched. ## Fix Two changes: 1. **Fall back to `__call__`** when no `__init__` is found. This correctly handles the [callable instance pattern](https://fastapi.tiangolo.com/advanced/advanced-dependencies/) from FastAPI's docs, where an instance with `__call__` is passed to `Depends`. 2. **Return `Some(Self::Unknown)`** instead of `None` when neither `__init__` nor `__call__` exists, so we conservatively suppress the diagnostic rather than emitting a false positive. ## Tests Added four new test cases: - Callable class with `__call__(self, thing_id)` → no diagnostic ✓ - Class with both `__init__(self, thing_id)` and `__call__` → no diagnostic (uses `__init__`) ✓ - Callable class where path param is NOT in `__call__` → FAST003 emitted ✓ - Empty class (no `__init__`, no `__call__`) → no diagnostic (Unknown) ✓ --------- Co-authored-by: stakeswky <stakeswky@users.noreply.github.com> Co-authored-by: User <user@example.com> Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
Author
Parents
Loading