Skip to content
tezvyn:

All bites

The whole library, newest first. Filter by what you are here for, or pick a topic if you already know.

8668 bites

Page 133

What is the difference between async def and regular functions?
Python & FastAPI2 min read

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.

Python & FastAPI2 min read

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.

Python & FastAPI2 min read

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.

Python & FastAPI2 min read

How do you dynamically discover and register FastAPI routers from a directory?

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?
Python & FastAPI2 min read

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.

Python & FastAPI2 min read

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.

Python & FastAPI2 min read

How to apply a dependency to only one FastAPI router?

Pass dependencies=[Depends(auth)] to APIRouter for /users, omit it for /items, include both.

How do you include a router in your main FastAPI app?
Python & FastAPI2 min read

How do you include a router in your main FastAPI app?

Create APIRouter in another file, import it into main.py, then call app.include_router(router).

Describe a common FastAPI project structure and key directories
Python & FastAPI2 min read

Describe a common FastAPI project structure and key directories

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?
Python & FastAPI2 min read

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
Python & FastAPI2 min read

Explain the internal role of the Depends class

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?
Python & FastAPI2 min read

How does lifecycle differ for global vs path operation dependencies?

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?
Python & FastAPI2 min read

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.

Python & FastAPI2 min read

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.

Python & FastAPI2 min read

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?
Python & FastAPI2 min read

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.

Python & FastAPI2 min read

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.

Python & FastAPI2 min read

How do you apply a dependency to an APIRouter without per-endpoint signatures?

Pass Depends() to APIRouter dependencies parameter; runs before every route in that router and shows in docs.

Python & FastAPI2 min read

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?
Python & FastAPI2 min read

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.