More in Python & FastAPI — page 5

What is the difference between async def and regular functions?
This tests async def call semantics. A strong answer contrasts regular functions, which execute immediately, with async def, which returns a coroutine object that does nothing until awaited or wrapped in create_task.
Override a FastAPI dependency at the APIRouter level
Tests FastAPI DI scoping limits. Answer: APIRouter has no dependency_overrides; create a sub-app, apply overrides, mount it. Red flag: Claiming router-level overrides exist or mutating global app state.
How do you manage service lifecycle with FastAPI Depends versus formal DI?
Tests scaling FastAPI DI beyond routes. Answer: use Depends(yield) for request-scoped DB sessions; use a formal container for deep singleton service graphs and lifespan wiring; hybrid is best. Red flag: using Depends for everything and ignoring testability.
How do you dynamically discover and register FastAPI routers from a directory?
WHAT IT TESTS: You can safely register FastAPI routers via dynamic imports. ANSWER OUTLINE: Scan app/routers/ with importlib, validate APIRouter objects, include_router with prefixes, and isolate failures.

How do you apply a common path prefix across FastAPI routers?
Tests whether you know APIRouter decouples routes from path prefixes. Use relative paths in APIRouter, then mount with app.include_router(router, prefix="/api/v1"). This keeps modules reusable. Red flag: hardcoding the full absolute path in every decorator.
Manage dev, staging, and prod configs in a large FastAPI app
This tests separating environment config from code with pydantic-settings and env vars. A strong answer covers .env files for local dev, per-environment validation, and secret injection for prod. Red flags are hardcoded values or scattered conditionals.
How to apply a dependency to only one FastAPI router?
TESTS: router-level dependency injection in FastAPI. OUTLINE: pass dependencies=[Depends(auth)] to APIRouter for /users, omit it for /items, include both. RED FLAG: repeating Depends() on every route or using middleware.

How do you include a router in your main FastAPI app?
WHAT IT TESTS: knowledge of the APIRouter wiring pattern. ANSWER OUTLINE: create APIRouter in another file, import it into main.py, then call app.include_router(router). RED FLAG: rewriting endpoints manually in main.py rather than using include_router.

Describe a common FastAPI project structure and key directories
WHAT IT TESTS: Scaling past one file with APIRouter and domain separation. ANSWER OUTLINE: Main module holds the FastAPI app; domain files (users, items) expose APIRouters; dependencies in their own module; pyproject.toml as entrypoint.

What is APIRouter in FastAPI and why use it?
It tests your grasp of modular architecture in FastAPI. APIRouter splits routes into separate modules so you include them in the main app with prefixes, tags, and dependencies.

Explain the internal role of the Depends class
WHAT IT TESTS: If you treat Depends as FastAPI's core DI declaration primitive. ANSWER OUTLINE: A strong answer notes it marks parameters for solver, enables recursive sub-dependencies and Annotated sharing, and feeds OpenAPI.

How does lifecycle differ for global vs path operation dependencies?
WHAT IT TESTS: Per-request scoping in FastAPI. ANSWER OUTLINE: Global deps run on every request to any route; path-local deps run only for that route; expensive setup belongs in a lifespan event or cached singleton, not a dependency.

How would you implement a dependency requiring multi-source parameters?
Tests if you know FastAPI resolves dependency params like endpoint params. Great answers annotate each parameter with its source inside the dependency so FastAPI injects them independently. Red flag: manually parsing Request or merging values in the endpoint.
How does FastAPI cache dependencies within a single request?
Tests if you know FastAPI caches a dependency after the first call in a request and reuses it across the tree. A strong answer covers default use_cache=True, request-scoped lifetime, and disabling it.
How does FastAPI execute setup and teardown in nested yield dependencies?
It tests FastAPI's dependency injection lifecycle and stack-like teardown for nested yield dependencies. Setup runs top-down; teardown runs bottom-up after the response. A red flag is claiming teardown order is arbitrary or follows garbage collection.

How do you use a Python class as a FastAPI dependency?
It tests whether you understand FastAPI DI beyond functions and when stateful encapsulation wins. Explain that Depends takes callable classes, centralizing setup and shared state in __init__. Red flag: claiming classes are pure syntactic sugar.
How would you override a FastAPI dependency during testing?
Tests your grasp of FastAPI's dependency override mechanism. A strong answer mentions app.dependency_overrides, notes that sub-dependencies are bypassed, and stresses clearing overrides after each test.
How do you apply a dependency to an APIRouter without per-endpoint signatures?
WHAT IT TESTS: FastAPI router-level DI. ANSWER OUTLINE: pass Depends() to APIRouter dependencies parameter; runs before every route in that router and shows in docs. RED FLAG: using middleware or manual decorators over the native dependencies argument.
What is the purpose of yield in a dependency function?
Tests teardown logic in FastAPI dependencies. Yield splits setup from cleanup: code before yield runs pre-request, after yield runs post-response to close resources like DB sessions. Red flag: confusing it with return or thinking yield is only for generators.

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.