Skip to content

fast-api-non-annotated-dependency (FAST002)

Derived from the FastAPI linter.

Fix is sometimes available.

This rule is unstable and in preview. The --preview flag is required for use.

What it does

Identifies FastAPI routes with deprecated uses of Depends.

Why is this bad?

The FastAPI documentation recommends the use of Annotated for defining route dependencies and parameters, rather than using Depends directly with a default value.

This approach is also suggested for various route parameters, including Body and Cookie, as it helps ensure consistency and clarity in defining dependencies and parameters.

Example

from fastapi import Depends, FastAPI

app = FastAPI()


async def common_parameters(q: str | None = None, skip: int = 0, limit: int = 100):
    return {"q": q, "skip": skip, "limit": limit}


@app.get("/items/")
async def read_items(commons: dict = Depends(common_parameters)):
    return commons

Use instead:

from typing import Annotated

from fastapi import Depends, FastAPI

app = FastAPI()


async def common_parameters(q: str | None = None, skip: int = 0, limit: int = 100):
    return {"q": q, "skip": skip, "limit": limit}


@app.get("/items/")
async def read_items(commons: Annotated[dict, Depends(common_parameters)]):
    return commons