tezvyn:

How do you declare a function as a dependency, and why?

AI-drafted, machine-checkedSource: fastapi.tiangolo.combeginner
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.

WHAT THIS TESTS: Whether you understand FastAPI's dependency injection system at a mechanical level. The interviewer wants to know if you can separate reusable logic from path operations and let the framework wire them together rather than hard-coding shared behavior inside every route. It also checks that you know the Depends import and that you grasp the inversion-of-control pattern.

A GOOD ANSWER COVERS: First, create a simple function that returns or validates something; this is the dependency, also called the dependable. The function can be sync or async because FastAPI handles both forms automatically. Second, import Depends from fastapi. Third, declare the dependency in the path operation parameters by assigning the parameter default to Depends(your_function) or by using Annotated with Depends for better type checking and sharing across endpoints. Fourth, explain the primary purpose: dependency injection means your path operation declares what it requires to work, and FastAPI takes care of doing whatever is needed to provide those dependencies. This keeps route handlers focused on HTTP concerns and pushes shared logic like authentication, database sessions, or query validation into reusable components that are easy to test and share.

COMMON WRONG ANSWERS: Calling the dependency function manually inside the path operation body instead of letting FastAPI inject it. Describing dependencies as just imported helper functions that you invoke yourself. Conflating dependencies with middleware, which runs on every request globally rather than being declared per route or per router. Thinking dependencies are only for authentication or security. Forgetting that the dependency itself can use Depends to form sub-dependencies. Not mentioning that the system is integrated with OpenAPI and appears in the automatic documentation.

LIKELY FOLLOW-UPS: How do sub-dependencies work when one Depends call uses another? Can a dependency be async and what are the performance implications? How do you share the same dependency across multiple routes without repeating the declaration? What is the difference between dependencies declared on the path operation function parameters versus the path operation decorator? How do dependencies with yield work for setup and cleanup, such as database sessions? When should you use a class instead of a function as a dependency?

ONE CONCRETE EXAMPLE: Imagine a function named common_parameters that accepts skip and limit query parameters and returns a dictionary. You import Depends from fastapi. In your path operation, you write something like async def read_items(commons: dict = Depends(common_parameters)). When a client hits the endpoint, FastAPI recognizes the dependency, calls common_parameters with the incoming query values, and passes the result into read_items as the commons argument. The route itself does not need to know how the parameters were parsed or validated. If another endpoint needs the same behavior, you reuse the same function and FastAPI resolves it independently for each request.

Source: fastapi.tiangolo.com

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.