Skip to content

api-schema-in-route-module (BTFAST002)

Added in 0.10.0 · Preview · Off by default (warning)

Family: fastapi · Standard: CES-17 · Languages: python

This rule is unstable and in preview. It runs only with preview = true, and only when it is selected.

Directive

A route module holds routes. A Pydantic schema (BaseModel) defined in a module under api/**/routers/ or api/**/routes/ is reported: put it in the schemas/requests/ or schemas/responses/ package of api. A warning by default.

Why

A schema inside a route file cannot be reused by another route or by a client, and the request/response split that the schemas packages express is lost.

Do

# api/routers/contracts.py
from myapp.api.schemas.requests.contract import CreateContractRequest

Don't

# api/routers/contracts.py
class CreateContract(BaseModel):
    name: str

Suppression

A comment on the class line silences the finding.

class CreateContract(BaseModel):  # botica: ignore[api-schema-in-route-module]
class CreateContract(BaseModel):  # noqa: BTFAST002 -- reason

Discussion