Software Engineer (SDE)fastapiintermediate3 interactive MCQs in app
Depends() & Function Dependencies
per-request Depends caching vs use_cache=Falsecomposable nested dependencies (auth + DB)test isolation with app.dependency_overrides
Core Concept
FastAPI `Depends()` builds clean composable dependency trees for auth (`get_current_user`), pagination (`PaginationParams`), and shared clients—with **per-request caching** by default (if two dependencies both declare `Depends(get_db)`, `get_db` runs ONCE per request cleanly!). Attach router/app-wide checks via `dependencies=[Depends(require_auth)]` even when handlers don’t need the return value!
Worked Example
if both the route and `get_current_user` declare `Depends(get_db)`, FastAPI caches `get_db` per request so only ONE DB session opens!
Common Pitfall
overriding dependencies by copying mocks into routes in tests instead of `app.dependency_overrides[get_current_user] = mock_user`.
Recommended Readings & Practice
Practice Depends() & Function Dependencies with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.