Bird
0
0

Identify the error in this FastAPI dependency code:

medium📝 Debug Q6 of 15
FastAPI - Dependency Injection
Identify the error in this FastAPI dependency code:
def get_config(name: str):
    return {'config_name': name}

@app.get('/config')
async def read_config(config: dict = Depends(get_config)):
    return config
ADepends cannot be used with functions returning dict
BReturn type of get_config must be a class instance
CMissing async keyword in get_config
Dget_config requires a parameter but none is provided in Depends
Step-by-Step Solution
Solution:
  1. Step 1: Check dependency function signature

    get_config requires a parameter 'name', but Depends is called without arguments.
  2. Step 2: Understand Depends parameter passing

    Depends expects a callable without required parameters or a dependency that can be resolved automatically.
  3. Final Answer:

    get_config requires a parameter but none is provided in Depends -> Option D
  4. Quick Check:

    Depends needs callable without required parameters [OK]
Quick Trick: Dependencies must have no required parameters or use nested functions [OK]
Common Mistakes:
MISTAKES
  • Passing function with required parameters directly to Depends
  • Assuming Depends can auto-fill parameters
  • Ignoring function signature requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes