Skip to content

Using a callable as a FastAPI dependency leads to false positive FAST003 errors #23526

@lerebear

Description

@lerebear

Summary

It appears that using a Callable (defined as a class with a __call__ method) as a FastAPI dependency produces false positive FAST003 errors: path parameters that are declared in the __call__ method signature are not properly recognized.

This is demonstrated in the following example:

# main.py

from fastapi import FastAPI, Depends
from typing import Annotated

app = FastAPI()


class Query:
    def __call__(self, id: str):
        pass


@app.get("/things/{id}")
async def get(thing: Annotated[str, Depends(Query)]):
    pass

For which uv run ruff check main.py --select "FAST" --isolated produces:

FAST003 Parameter `id` appears in route path, but not in `get` signature
  --> main.py:13:19
   |
13 | @app.get("/things/{id}")
   |                   ^^^^
14 | async def get(thing: Annotated[str, Depends(Query)]):
15 |     pass
   |
help: Add `id` to function signature

Found 1 error.
No fixes available (1 hidden fix can be enabled with the `--unsafe-fixes` option).

By contrast, using a regular function as the dependency does not produce a false positive:

from fastapi import FastAPI, Depends
from typing import Annotated

app = FastAPI()


def query(id: str):
    pass


@app.get("/things/{id}")
async def get(thing: Annotated[str, Depends(query)]):
    pass

Version

ruff 0.15.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions