astral-sh/ruff

`FAST003` false positive when path parameter is included inside a `BaseModel` dependency

Ouverte

#17 226 ouverte le 5 avr. 2025

 (3 commentaires) (0 réaction) (0 personne assignée)Rust (2 088 forks)batch import
help wantedruletype-inference

Métriques du dépôt

Stars
 (47 527 étoiles)
Métriques de merge PR
 (Merge moyen 3j 8h) (573 PRs mergées en 30 j)

Description

Summary

I’ve noticed that the rule FAST003 (fast-api-unused-path-parameter) is reporting false positives when the path parameter is provided via a BaseModel using Depends.

For example, consider the following code:

from fastapi import APIRouter, Depends
from pydantic import BaseModel
from typing import Annotated

router = APIRouter()

class MyParams(BaseModel):
    my_id: int

@router.get("/{my_id}")
async def get_id(params: Annotated[MyParams, Depends()]):
    return {"my_id": params.my_id}

In this case, my_id is being used correctly via the params dependency, but the linter still raises: Parameter my_id appears in route path, but not in get_id signature Ruff [FAST003]

However, this is a valid and common FastAPI pattern, where parameters are parsed from Depends() into a Pydantic model.

Expected behavior

No warning should be raised when the path parameter is being handled inside a BaseModel via dependency injection.

Actual behavior

The linter raises FAST003 even though the parameter is properly used.

Additional notes

  • This pattern is especially useful for grouping parameters together.
  • The rule might need to inspect Depends() models to detect path parameters coming from annotated dependencies.

Versions

Ruff: 0.11.3 FastAPI: 0.115.12 Pydantic: 2.11.2

Version

0.11.3

Guide contributeur