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.
WHAT THIS TESTS: This question probes whether you view FastAPI dependencies as a real architectural layer or merely as a bag of helper functions. The interviewer wants to see that you understand the dependency injection system can host callable classes, not just bare functions, and that you know when object-oriented encapsulation improves maintainability over a purely procedural approach. They are also checking if you grasp how FastAPI resolves dependencies by calling the provided callable and how that design naturally extends to classes with initialization logic.
A GOOD ANSWER COVERS: First, explain that Depends will invoke any callable, so a class exposing a __call__ method works exactly like a function dependency while adding the power of object initialization. Second, describe how __init__ becomes the natural place for sub-dependencies and configuration, turning the class into a reusable service boundary that accepts parameters at the router or endpoint level. Third, highlight concrete benefits: centralized setup logic that avoids repetition, shared mutable or immutable state across multiple path operations, cleaner unit testing through standard instantiation without mocking frameworks, and seamless integration with OpenAPI because FastAPI continues to inspect type annotations on __call__. Fourth, contrast this with function-based dependencies, which are inherently stateless and can scatter setup boilerplate when the same preamble is needed in many endpoints, forcing developers to rely on closures or repeated parameter lists.
COMMON WRONG ANSWERS: A major red flag is claiming classes are only syntactic sugar and provide zero runtime or organizational benefit. Another is forgetting that FastAPI manages dependency resolution automatically, so developers should not manually instantiate the class themselves instead of letting the framework inject it through Depends. Some candidates also confuse class instantiation with dependency scope, incorrectly arguing that class-based dependencies leak state between requests unless manually managed.
LIKELY FOLLOW-UPS: The interviewer may ask how you would share a database connection pool via a class dependency, how to override a class dependency during unit or integration testing using dependency overrides, or whether you should prefer a class over a factory function when the setup logic grows complex. They might also ask how sub-dependencies interact with class initialization or how type hints on __call__ affect automatic documentation.
ONE CONCRETE EXAMPLE: Imagine a PermissionChecker class that accepts a list of required roles in __init__, receives the current user from a sub-dependency, and returns a boolean or raises an HTTPException in __call__. Using a class keeps the role configuration declarative at the router level while encapsulating the authorization logic in one testable unit, something a single function cannot do as cleanly without awkward closures or repeated parameters across every protected endpoint.
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.