0
0
FastAPIframework~10 mins

Dependencies with parameters in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a dependency function with a parameter.

FastAPI
from fastapi import Depends, FastAPI

app = FastAPI()

def common_parameters(q: str = [1]):
    return {"q": q}

@app.get("/items/")
async def read_items(commons: dict = Depends(common_parameters)):
    return commons
Drag options to blanks, or click blank then click option'
A"default"
BFalse
CNone
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string like "default" instead of None for optional parameters.
Forgetting to set a default value, making the parameter required.
2fill in blank
medium

Complete the code to inject a dependency with a parameter into a path operation.

FastAPI
from fastapi import Depends, FastAPI

app = FastAPI()

def query_extractor(q: str = None):
    return q

@app.get("/search/")
async def search(q: str = Depends([1])):
    return {"query": q}
Drag options to blanks, or click blank then click option'
Aquery_extractor
BDepends
CFastAPI
Dsearch
Attempts:
3 left
💡 Hint
Common Mistakes
Passing Depends itself instead of the dependency function.
Passing the FastAPI app instance instead of a function.
3fill in blank
hard

Fix the error in the dependency function parameter default value.

FastAPI
from fastapi import Depends, FastAPI

app = FastAPI()

def pagination(skip: int = [1]):
    return {"skip": skip}

@app.get("/items/")
async def list_items(pagination: dict = Depends(pagination)):
    return pagination
Drag options to blanks, or click blank then click option'
A"0"
BNone
CFalse
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string "0" instead of integer 0 as default.
Using None for a parameter that expects an int without Optional.
4fill in blank
hard

Fill both blanks to create a dependency with two parameters and use it in a path operation.

FastAPI
from fastapi import Depends, FastAPI

app = FastAPI()

def pagination(skip: int = 0, limit: int = [1]):
    return {"skip": skip, "limit": limit}

@app.get("/items/")
async def list_items(pagination: dict = Depends([2])):
    return pagination
Drag options to blanks, or click blank then click option'
A10
Bpagination
C5
Dlist_items
Attempts:
3 left
💡 Hint
Common Mistakes
Using the path operation function name instead of the dependency function.
Setting limit default to a string instead of an integer.
5fill in blank
hard

Fill all three blanks to create a dependency with parameters and use it with type hints in the path operation.

FastAPI
from fastapi import Depends, FastAPI

app = FastAPI()

def query_params(q: str = None, skip: int = 0, limit: int = [1]):
    return {"q": q, "skip": skip, "limit": limit}

@app.get("/search/")
async def search_items(params: dict = Depends([2])) -> [3]:
    return params
Drag options to blanks, or click blank then click option'
A20
Bquery_params
Cdict
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect default values for limit.
Passing the wrong function to Depends.
Using wrong or missing return type hints.