How do you declare a function as a dependency, and why?

Tests FastAPI dependency injection basics. Answer: create a function, import Depends, and add it to path operation parameters so FastAPI injects it. Purpose: routes declare what they need instead of hard-coding shared logic.
Tests FastAPI dependency injection mechanics and the exact role of the Depends callable. Strong answer: write a dependency function, import Depends from fastapi, and add it to path operation parameters so FastAPI resolves it automatically. The purpose is inversion of control: routes declare what they need and the framework injects it, keeping endpoints thin and shared logic reusable across the app. Red flag: calling the dependency manually inside the route or confusing it with middleware.
Read the original → fastapi.tiangolo.com
- #fastapi
- #dependency-injection
- #python
- #beginner
- #depends
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.