0
0
FastAPIframework~20 mins

Shared dependencies in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Shared 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 shared dependency example?

Consider this FastAPI code using a shared dependency:

from fastapi import FastAPI, Depends

app = FastAPI()

async def common_dep():
    return "shared value"

@app.get("/one")
async def one(dep=Depends(common_dep)):
    return {"result": dep}

@app.get("/two")
async def two(dep=Depends(common_dep)):
    return {"result": dep + " appended"}

What will be the JSON response from the endpoint /two?

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

async def common_dep():
    return "shared value"

@app.get("/one")
async def one(dep=Depends(common_dep)):
    return {"result": dep}

@app.get("/two")
async def two(dep=Depends(common_dep)):
    return {"result": dep + " appended"}
A{"result": "shared value appended appended"}
B{"result": "shared value"}
C{"result": "shared value appended"}
D{"result": null}
Attempts:
2 left
💡 Hint

Think about what the dependency common_dep returns and how it is used in the /two endpoint.

state_output
intermediate
2:00remaining
How many times is the shared dependency called in this FastAPI example?

Given this FastAPI code:

from fastapi import FastAPI, Depends

app = FastAPI()

call_count = 0

async def shared_dep():
    global call_count
    call_count += 1
    return call_count

@app.get("/first")
async def first(dep=Depends(shared_dep)):
    return {"count": dep}

@app.get("/second")
async def second(dep=Depends(shared_dep)):
    return {"count": dep}

If a client calls /first and then /second in sequence, what will be the value of call_count after both calls?

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

call_count = 0

async def shared_dep():
    global call_count
    call_count += 1
    return call_count

@app.get("/first")
async def first(dep=Depends(shared_dep)):
    return {"count": dep}

@app.get("/second")
async def second(dep=Depends(shared_dep)):
    return {"count": dep}
A0
B2
C3
D1
Attempts:
2 left
💡 Hint

Each endpoint call triggers the dependency function once.

🔧 Debug
advanced
2:00remaining
What error does this FastAPI shared dependency code raise?

Examine this FastAPI code snippet:

from fastapi import FastAPI, Depends

app = FastAPI()

async def dep():
    return 42

@app.get("/error")
async def error(dep=Depends(dep())):
    return {"value": dep}

What error will occur when starting or calling the /error endpoint?

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

async def dep():
    return 42

@app.get("/error")
async def error(dep=Depends(dep())):
    return {"value": dep}
ATypeError: 'int' object is not callable
BNo error, returns {"value": 42}
CSyntaxError: invalid syntax
DRuntimeError: Dependency function called at import time
Attempts:
2 left
💡 Hint

Check how the dependency is passed to Depends.

🧠 Conceptual
advanced
2:00remaining
Which option correctly shares a database session dependency in FastAPI?

You want to share a database session dependency across multiple endpoints in FastAPI. Which code snippet correctly implements this shared dependency?

A
def get_db():
    db = Session()
    try:
        yield db
    finally:
        db.close()

@app.get("/items")
async def read_items(db=Depends(get_db)):
    return db.query(Item).all()
B
def get_db():
    db = Session()
    return db
    db.close()

@app.get("/items")
async def read_items(db=Depends(get_db)):
    return db.query(Item).all()
C
def get_db():
    db = Session()
    yield db
    db.close()

@app.get("/items")
async def read_items(db=Depends(get_db())):
    return db.query(Item).all()
D
def get_db():
    db = Session()
    yield db

@app.get("/items")
async def read_items(db=Depends(get_db)):
    return db.query(Item).all()
Attempts:
2 left
💡 Hint

Think about how to properly open and close resources with dependencies.

📝 Syntax
expert
2:00remaining
What is the correct syntax to declare a shared dependency with parameters in FastAPI?

Given a dependency function that accepts parameters, which option correctly declares it for use as a shared dependency in FastAPI?

def common_dep(param: int):
    return param * 2
FastAPI
def common_dep(param: int):
    return param * 2
A
@app.get("/calc")
async def calc(value: int, result=Depends(lambda: common_dep(value))):
    return {"result": result}
B
@app.get("/calc")
async def calc(value: int, result=Depends(common_dep)):
    return {"result": result}
C
@app.get("/calc")
async def calc(value: int, result=Depends(common_dep(value))):
    return {"result": result}
D
}tluser :"tluser"{ nruter    
:))ped_nommoc(sdnepeD=tluser ,tni :eulav(clac fed cnysa
)"clac/"(teg.ppa@
Attempts:
2 left
💡 Hint

Consider how to pass parameters to dependencies dynamically.