0
0
FastAPIframework~20 mins

Class-based dependencies in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Class-based Dependency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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()
A{"item_id": "42", "name": "Item 42"}
BTypeError: __init__() missing 1 required positional argument: 'item_id'
C{"item_id": 42}
D{"item_id": 42, "name": "Item 42"}
Attempts:
2 left
💡 Hint
Think about how FastAPI injects path parameters into class dependencies.
📝 Syntax
intermediate
2: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.
A
class UserDependency:
    def __call__(self, user_id: str):
        return {"user_id": int(user_id)}

@app.get("/users/{user_id}")
async def get_user(dep: UserDependency = Depends(UserDependency)):
    return dep
B
class UserDependency:
    def __call__(self):
        return {"user_id": user_id}

@app.get("/users/{user_id}")
async def get_user(dep: UserDependency = Depends(UserDependency)):
    return dep
C
class UserDependency:
    def __init__(self, user_id: int):
        self.user_id = user_id

@app.get("/users/{user_id}")
async def get_user(dep: UserDependency = Depends(UserDependency)):
    return {"user_id": dep.user_id}
D
class UserDependency:
    def __call__(self, user_id: int):
        return {"user_id": user_id}

@app.get("/users/{user_id}")
async def get_user(dep: UserDependency = Depends(UserDependency)):
    return dep
Attempts:
2 left
💡 Hint
The __call__ method must accept the parameter and return the expected data.
🔧 Debug
advanced
2: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()
AThe endpoint returns the class instance instead of calling it, causing a TypeError.
BThe __call__ method is missing the 'order_id' parameter, causing a runtime error.
CFastAPI cannot pass 'order_id' to the __init__ method automatically, causing a TypeError.
DThe Depends function is used incorrectly without parentheses, causing a syntax error.
Attempts:
2 left
💡 Hint
Check how FastAPI injects parameters into class dependencies.
state_output
advanced
2: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}
ATypeError: __init__() missing 1 required positional argument: 'number'
B20
C10
D0
Attempts:
2 left
💡 Hint
How does FastAPI pass path parameters to class constructors?
🧠 Conceptual
expert
2:00remaining
Which statement about FastAPI class-based dependencies is TRUE?
Select the correct statement about how FastAPI handles class-based dependencies with parameters.
ATo receive parameters, a class-based dependency should implement a __call__ method with parameters matching the route parameters.
BFastAPI automatically injects path and query parameters into the __init__ constructor of dependency classes without extra configuration.
CClass-based dependencies cannot access request data unless explicitly passed via constructor parameters.
DUsing class-based dependencies disables FastAPI's automatic validation of parameters.
Attempts:
2 left
💡 Hint
Think about how FastAPI calls class dependencies to inject parameters.