0
0
FastAPIframework~20 mins

Path operation dependencies in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI 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 path operation with dependencies?

Consider this FastAPI code snippet:

from fastapi import FastAPI, Depends

app = FastAPI()

def common_parameters(q: str | None = None):
    return {"q": q}

@app.get("/items/")
async def read_items(commons: dict = Depends(common_parameters)):
    return commons

What will be the JSON response when a client requests /items/?q=hello?

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

def common_parameters(q: str | None = None):
    return {"q": q}

@app.get("/items/")
async def read_items(commons: dict = Depends(common_parameters)):
    return commons
A{"q": "hello"}
BHTTP 422 Unprocessable Entity error
C{}
D{"q": null}
Attempts:
2 left
💡 Hint

Think about how query parameters are passed and how the dependency function returns them.

state_output
intermediate
2:00remaining
What is the value of 'user' inside the path operation?

Given this FastAPI code:

from fastapi import FastAPI, Depends

app = FastAPI()

def get_user(token: str):
    if token == "secret":
        return "admin"
    return "guest"

@app.get("/profile")
async def profile(user: str = Depends(get_user)):
    return {"user": user}

What will be the response when a client calls /profile?token=secret?

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

def get_user(token: str):
    if token == "secret":
        return "admin"
    return "guest"

@app.get("/profile")
async def profile(user: str = Depends(get_user)):
    return {"user": user}
A{"user": "guest"}
B{"user": "admin"}
CHTTP 422 Unprocessable Entity error
D{"user": null}
Attempts:
2 left
💡 Hint

Check how the token query parameter is passed to the dependency.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in FastAPI dependency declaration?

Which of the following FastAPI dependency declarations will cause a syntax error?

Adef dep(q: str = Depends()): return q
Bq nruter :))(sdnepeD = rts :q(ped fed
Cq nruter :)sdnepeD = rts :q(ped fed
Ddef dep(q: str = Depends): return q
Attempts:
2 left
💡 Hint

Check how Depends is used as a callable.

lifecycle
advanced
2:00remaining
How many times is the dependency function called per request?

Given this FastAPI code:

from fastapi import FastAPI, Depends

app = FastAPI()

call_count = 0

def dep():
    global call_count
    call_count += 1
    return call_count

@app.get("/count")
async def count_calls(a: int = Depends(dep), b: int = Depends(dep)):
    return {"a": a, "b": b}

How many times is dep called when a client requests /count?

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

call_count = 0

def dep():
    global call_count
    call_count += 1
    return call_count

@app.get("/count")
async def count_calls(a: int = Depends(dep), b: int = Depends(dep)):
    return {"a": a, "b": b}
A2
B1
C0
DDepends on caching settings
Attempts:
2 left
💡 Hint

Each Depends call creates a separate dependency instance.

🔧 Debug
expert
2:00remaining
Why does this FastAPI dependency cause a runtime error?

Examine this FastAPI code snippet:

from fastapi import FastAPI, Depends

app = FastAPI()

def get_token(token: str):
    return token

@app.get("/secure")
async def secure_route(token: str = Depends(get_token)):
    return {"token": token}

When calling /secure without query parameters, what error occurs and why?

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

def get_token(token: str):
    return token

@app.get("/secure")
async def secure_route(token: str = Depends(get_token)):
    return {"token": token}
AReturns {"token": null}
BHTTP 500 Internal Server Error due to missing dependency
CHTTP 422 Unprocessable Entity because 'token' is missing
DReturns {"token": ""} (empty string)
Attempts:
2 left
💡 Hint

Consider how FastAPI handles required query parameters in dependencies.