Bird
0
0

Identify the error in this FastAPI dependency usage:

medium📝 Debug Q14 of 15
FastAPI - Dependency Injection
Identify the error in this FastAPI dependency usage:
def get_limit(limit: int = 10):
    return limit

@app.get("/items")
async def read_items(limit = Depends(get_limit(limit=20))):
    return {"limit": limit}
ADefault value in get_limit conflicts with parameter passed
BCannot pass parameters directly inside Depends like get_limit(limit=20)
CMissing type annotation for limit in read_items
DDepends should be imported from fastapi.dependencies
Step-by-Step Solution
Solution:
  1. Step 1: Check Depends usage

    Depends expects a callable or a call to a callable without parameters directly inside Depends.
  2. Step 2: Correct way to pass parameters

    To pass parameters, wrap get_limit in another function or use a lambda to supply parameters.
  3. Final Answer:

    Cannot pass parameters directly inside Depends like get_limit(limit=20) -> Option B
  4. Quick Check:

    Depends() must wrap callable, not call with parameters directly [OK]
Quick Trick: Wrap parameterized dependency call outside Depends() [OK]
Common Mistakes:
MISTAKES
  • Calling dependency with parameters inside Depends directly
  • Ignoring need for wrapper function
  • Wrong import path for Depends

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes