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.

8664 bites

Page 5

Python & FastAPI2 min read

FastAPI: Set Cookies Without Returning a Response Object

Inject a Response object into your endpoint to set cookies without manually building the whole response. Use this for session tokens while still returning data like a dict.

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.

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.

Python & FastAPI2 min read

FastAPI `yield` Dependencies for Setup and Teardown

A yield dependency is a context manager for your endpoints. Code before yield runs setup, like getting a DB connection; code after yield runs teardown.

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).

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.

Python & FastAPI2 min read

FastAPI: Setting Custom Response Headers

Set custom HTTP headers in FastAPI by adding a Response parameter to your endpoint. This lets you add metadata like trace IDs without changing your return data. The footgun is thinking you must return the Response object; just return your data as usual.

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.

FastAPI's Depends: Let the Framework Handle Setup
Python & FastAPI2 min read

FastAPI's Depends: Let the Framework Handle Setup

Think of Depends as a pre-flight checklist for your API endpoints. You list required setup tasks, like getting a user or a database session, and FastAPI runs them for you. This is key for sharing logic like auth or database connections across many routes.

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.

FastAPI: Using Classes as Dependencies
Python & FastAPI2 min read

FastAPI: Using Classes as Dependencies

Bundle related request parameters into a class instead of repeating them in every endpoint. FastAPI automatically creates an instance for you, cleaning up your code. This is ideal for shared logic like pagination. The footgun: FastAPI injects into __init__.

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.

Python & FastAPI2 min read

FastAPI's Dependency Caching: One Request, One Call

FastAPI dependencies are singletons for the life of a request. If multiple parts of your code ask for the same dependency (e.g., a database session), FastAPI runs it once, caches the result, and shares it. The footgun: this cache is per-request, not global.

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

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

FastAPI Global Dependencies: DRY Your API Logic

A FastAPI global dependency is like a bouncer for your entire API, running a check on every request. Use it for universal concerns like API key validation. The footgun is applying logic that should only affect a subset of routes, making your API rigid.

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.

Explain await's purpose, awaitable types, and event loop signaling
Python & FastAPI2 min read

Explain await's purpose, awaitable types, and event loop signaling

This tests whether you understand await as a yield point. A strong answer lists three awaitables (coroutines, Tasks, Futures), explains await yields control to the event loop until completion, and warns that a bare coroutine call does not run it.

How do you safely execute blocking code from an async function
Python & FastAPI2 min read

How do you safely execute blocking code from an async function

Tests whether you know how to prevent event loop blocking by offloading sync work. Name asyncio.to_thread or run_in_executor, explain ThreadPoolExecutor scheduling, and note when processes beat threads.

Python & FastAPI2 min read

FastAPI's Security Utility: Dependencies for Auth

FastAPI's Security utility is a specialized Depends for authentication. It signals to OpenAPI that a dependency is required for security, enabling interactive docs. Use it to protect endpoints by injecting the authenticated user.