Challenge - 5 Problems
FastAPI Sub-dependencies 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 sub-dependency example?
Consider the following FastAPI code with sub-dependencies. What will be the JSON response when a GET request is made to
/items/42?FastAPI
from fastapi import FastAPI, Depends app = FastAPI() def get_user(): return {"user_id": 1, "name": "Alice"} def get_item(user: dict = Depends(get_user), item_id: int = 0): return {"item_id": item_id, "owner": user["name"]} @app.get("/items/{item_id}") async def read_item(item: dict = Depends(get_item)): return item
Attempts:
2 left
💡 Hint
Think about how the path parameter
item_id is passed through dependencies.✗ Incorrect
The
item_id path parameter is passed automatically to get_item because it matches the parameter name. The get_user dependency provides the user dict. So the final response includes item_id 42 and owner "Alice".❓ lifecycle
intermediate2:00remaining
How many times is the sub-dependency called?
Given this FastAPI code, how many times will
get_db be called when a GET request is made to /users/5?FastAPI
from fastapi import FastAPI, Depends app = FastAPI() call_count = {"db": 0} def get_db(): call_count["db"] += 1 return "db_connection" def get_user(db=Depends(get_db), user_id: int = 0): return {"user_id": user_id, "db": db} @app.get("/users/{user_id}") async def read_user(user=Depends(get_user)): return user
Attempts:
2 left
💡 Hint
Consider how FastAPI resolves dependencies and sub-dependencies per request.
✗ Incorrect
The
get_db dependency is called once per request. It is a sub-dependency of get_user. So when read_user is called, get_user calls get_db once. The call count is 1.🔧 Debug
advanced2:00remaining
Why does this FastAPI sub-dependency raise a validation error?
Examine this code snippet. When calling
/orders/10, a validation error occurs. What causes it?FastAPI
from fastapi import FastAPI, Depends from pydantic import BaseModel app = FastAPI() class User(BaseModel): id: int name: str def get_user(): return {"id": 1, "name": "Bob"} def get_order(user: User = Depends(get_user), order_id: int = 0): return {"order_id": order_id, "user_name": user.name} @app.get("/orders/{order_id}") async def read_order(order=Depends(get_order)): return order
Attempts:
2 left
💡 Hint
Check the type expected by the
user parameter in get_order.✗ Incorrect
The
get_order function expects user to be a User model instance. But get_user returns a plain dict. FastAPI tries to validate the dict as a User but fails, raising a ValidationError.📝 Syntax
advanced2:00remaining
Which option correctly defines a sub-dependency with parameters?
You want to create a sub-dependency
get_token that depends on get_header and also accepts a parameter required: bool. Which option is correct?Attempts:
2 left
💡 Hint
In Python, parameters with default values must come after those without defaults.
✗ Incorrect
Option A correctly places the parameter with default
required: bool = True before the dependency parameter with default header=Depends(get_header). This matches Python's rule that parameters without defaults cannot follow those with defaults. Options A and D have parameters without defaults after those with defaults, causing syntax errors. Option A places required before header but without a default, which is valid but less flexible.❓ state_output
expert3:00remaining
What is the final value of
counter['calls'] after two requests?Given this FastAPI app with sub-dependencies and a mutable state counter, what is the value of
counter['calls'] after two separate GET requests to /process/1 and /process/2?FastAPI
from fastapi import FastAPI, Depends app = FastAPI() counter = {"calls": 0} def increment_counter(): counter["calls"] += 1 return counter["calls"] def process_item(call_count=Depends(increment_counter), item_id: int = 0): return {"item_id": item_id, "call_count": call_count} @app.get("/process/{item_id}") async def process_route(result=Depends(process_item)): return result
Attempts:
2 left
💡 Hint
Consider that the
counter dictionary is global and mutable.✗ Incorrect
The
increment_counter dependency increments the global counter['calls'] each time it is called. Each GET request to /process/{item_id} calls process_item, which depends on increment_counter. So after two requests, counter['calls'] is incremented twice, resulting in 2.