Community Question Bundle

FastAPI Dependency Injection Drill

A four-question drill on FastAPI's Depends system: caching, sub-dependencies, override patterns, and yield-based teardown. Aimed at developers preparing for a Python web backend interview.

FastAPI Dependency Injection Drill

A four-question drill on FastAPI's Depends system: caching, sub-dependencies, override patterns, and yield-based teardown. Aimed at developers preparing for a Python web backend interview.

Question Bundle
Python
4 questions
framework
py-asyncio
interview-prep

By CodeSnatch

April 3, 2026

·

Updated May 18, 2026

328 views

8

4.3 (13)

A FastAPI endpoint declares Depends(get_current_user) and so does a helper sub-dependency on the same request. Is get_current_user invoked once or twice per request, and what controls that?

Examples

Example 1:

Input: Request to /me with both route and require_admin depending on Depends(get_current_user)
Output: 'resolving user' prints once
Explanation: FastAPI caches each dependency once per request keyed by the callable identity.

Example 2:

Input: Replace one with Depends(get_current_user, use_cache=False)
Output: 'resolving user' prints twice
Explanation: Opting out of per-request caching forces a fresh evaluation for that dependency.