Challenge - 5 Problems
Class-based Dependency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this FastAPI class-based dependency?
Consider this FastAPI dependency class and endpoint. What will be the JSON response when accessing
/items/42?FastAPI
from fastapi import FastAPI, Depends app = FastAPI() class ItemDependency: def __init__(self, item_id: int): self.item_id = item_id def get_item(self): return {"item_id": self.item_id, "name": f"Item {self.item_id}"} @app.get("/items/{item_id}") async def read_item(dep: ItemDependency = Depends(ItemDependency)): return dep.get_item()
Attempts:
2 left
💡 Hint
Think about how FastAPI injects path parameters into class dependencies.
✗ Incorrect
FastAPI cannot automatically pass the path parameter 'item_id' to the class constructor unless explicitly told. The dependency injection fails because the constructor requires 'item_id' but FastAPI does not know to provide it.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a class-based dependency with a __call__ method in FastAPI?
Select the code snippet that correctly implements a class-based dependency with a __call__ method that accepts a path parameter
user_id.Attempts:
2 left
💡 Hint
The __call__ method must accept the parameter and return the expected data.
✗ Incorrect
Option D correctly defines __call__ with user_id parameter typed as int, matching the path parameter. It returns a dictionary with user_id. FastAPI injects user_id automatically.
🔧 Debug
advanced2:00remaining
Why does this class-based dependency raise a runtime error?
Examine the code below. When calling
/orders/5, it raises an error. What is the cause?FastAPI
from fastapi import FastAPI, Depends app = FastAPI() class OrderDependency: def __init__(self, order_id: int): self.order_id = order_id def __call__(self): return {"order_id": self.order_id} @app.get("/orders/{order_id}") async def get_order(dep: OrderDependency = Depends(OrderDependency)): return dep()
Attempts:
2 left
💡 Hint
Check how FastAPI injects parameters into class dependencies.
✗ Incorrect
FastAPI does not automatically inject path parameters into the __init__ constructor of a class dependency. This causes a TypeError because order_id is required but not provided.
❓ state_output
advanced2:00remaining
What is the value of
dep.value after this request?Given the class-based dependency below, what will be the value of
dep.value after a GET request to /calc/10?FastAPI
from fastapi import FastAPI, Depends app = FastAPI() class Calculator: def __init__(self, number: int): self.value = number * 2 @app.get("/calc/{number}") async def calculate(dep: Calculator = Depends(Calculator)): return {"result": dep.value}
Attempts:
2 left
💡 Hint
How does FastAPI pass path parameters to class constructors?
✗ Incorrect
FastAPI cannot automatically pass the path parameter 'number' to the Calculator constructor, so it raises a TypeError.
🧠 Conceptual
expert2:00remaining
Which statement about FastAPI class-based dependencies is TRUE?
Select the correct statement about how FastAPI handles class-based dependencies with parameters.
Attempts:
2 left
💡 Hint
Think about how FastAPI calls class dependencies to inject parameters.
✗ Incorrect
FastAPI calls the class instance if it has a __call__ method and injects parameters into that method. It does not inject parameters into the constructor automatically.