Bird
0
0

Identify the error in the following FastAPI code using dependencies:

medium📝 Debug Q14 of 15
FastAPI - Dependency Injection
Identify the error in the following FastAPI code using dependencies:
from fastapi import FastAPI, Depends

app = FastAPI()

def get_user():
    return "user1"

@app.get("/profile")
def profile(user: str = Depends(get_user)):
    return {"user": user}

@app.get("/dashboard")
def dashboard(user = Depends(get_user)):
    return {"dashboard_user": user}
AMissing type annotation for 'user' in dashboard function
BDepends() used incorrectly without parentheses
Cget_user function missing return statement
DPath operation decorator missing on dashboard function
Step-by-Step Solution
Solution:
  1. Step 1: Compare both path operation functions

    The profile function declares user: str = Depends(get_user) with a type annotation.

  2. Step 2: Identify the issue in dashboard

    The dashboard function uses user = Depends(get_user) but lacks a type annotation, which FastAPI requires for dependencies.

  3. Final Answer:

    Missing type annotation for 'user' in dashboard function -> Option A
  4. Quick Check:

    Dependency parameters need type annotations [OK]
Quick Trick: Always add type annotations for Depends parameters [OK]
Common Mistakes:
MISTAKES
  • Omitting type annotations on dependency parameters
  • Forgetting parentheses on Depends()
  • Assuming missing decorator causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes