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?
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"}
Think about what the dependency common_dep returns and how it is used in the /two endpoint.
The dependency common_dep returns the string "shared value". The /two endpoint appends " appended" to this string, so the final result is "shared value appended".
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?
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}
Each endpoint call triggers the dependency function once.
Each endpoint calls shared_dep once, so after two calls, call_count is 2.
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?
from fastapi import FastAPI, Depends app = FastAPI() async def dep(): return 42 @app.get("/error") async def error(dep=Depends(dep())): return {"value": dep}
Check how the dependency is passed to Depends.
The code calls dep() immediately when defining the dependency, so the async function is executed at import time, which FastAPI does not allow. This causes a runtime error.
You want to share a database session dependency across multiple endpoints in FastAPI. Which code snippet correctly implements this shared dependency?
Think about how to properly open and close resources with dependencies.
Option A uses a generator with try/finally to ensure the database session is closed after use. This is the recommended pattern for shared dependencies managing resources.
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 * 2def common_dep(param: int): return param * 2
Consider how to pass parameters to dependencies dynamically.
Option A uses a lambda to call common_dep with the parameter value at runtime, which is the correct way to pass parameters to dependencies.