Install & Compatibility
Where this runs
tested against v0.8.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
build_error
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 5.0s · import 0.000s · 56MB
56MB installed
● package 56MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
cbv
✓ from fastapi_utils.cbv import cbv
✗ from fastapi_utils.cbv import cbv
This quickstart demonstrates the use of Class-Based Views (CBV) to group related endpoints and share dependencies, a core feature of fastapi-utils. Methods within the `@cbv` decorated class become endpoints on the included router, and class-level `Depends` declarations are injected as instance attributes.
from fastapi import FastAPI, Depends, APIRouter
from fastapi_utils.cbv import cbv
from fastapi_utils.inferring_router import InferringRouter # Still commonly seen, but prefer APIRouter
app = FastAPI()
router = InferringRouter() # Or APIRouter() for newer FastAPI versions
def common_dependency():
return "Shared Data"
@cbv(router)
class ClassBasedView:
# Dependencies shared by all methods in this class
shared_data: str = Depends(common_dependency)
@router.get("/items/")
def read_items(self):
return {"message": f"Hello from CBV, shared data: {self.shared_data}"}
@router.post("/items/")
def create_item(self, item_name: str):
return {"message": f"Item '{item_name}' created with shared data: {self.shared_data}"}
app.include_router(router)
# To run: uvicorn main:app --reload (assuming this code is in main.py)
Debug
Known issues
deprecatedThe `InferringRouter` class was deprecated in `fastapi-utils` v0.4.4. Its core functionality for inferring response models is now a built-in feature of `fastapi.APIRouter` in recent FastAPI versions.fixReplace `from fastapi_utils.inferring_router import InferringRouter` with `from fastapi import APIRouter` and use it directly.
affects: >=0.4.4
gotchaWhen using `@cbv` (Class-Based Views), any dependencies defined as class attributes will be resolved and injected for *every* endpoint method within that class, even if a specific method doesn't explicitly use `self.dependency_name`. This can lead to unnecessary work if dependencies are expensive and not universally required.fixStructure your CBV classes to group endpoints with genuinely shared dependencies, or move non-universally required dependencies to the method signature of specific endpoints.
affects: All versions
gotchaFor `repeat_every` background tasks, if you're using a modern FastAPI version (0.68.0+), `app.on_event("startup")` is deprecated in favor of `asynccontextmanager` for managing application lifespan.fixAdapt your application to use `asynccontextmanager` for lifespan events and call your `repeat_every` decorated functions within the `startup` block of the lifespan context. Refer to FastAPI's official documentation on lifespan events.
affects: FastAPI >=0.68.0
breakingfastapi-utils v0.4.0 introduced a breaking change by removing support for Python versions < 3.6.2. Ensure your project runs on Python 3.8+ as per current requirements.fixUpgrade your Python environment to 3.8 or newer. The current version (0.8.0) requires Python >=3.8.
affects: <0.4.0
Upgrade
Version history
0.8.0latest on PyPI · released Nov 11, 2024
Audit
Dependencies
fastapirequiredCore framework the utilities extend.
pydanticrequiredUsed for data validation and settings, required by FastAPI.
sqlalchemyoptionalRequired for `FastAPISessionMaker` functionality.