Bird
0
0

Identify the issue in the following FastAPI code snippet:

medium📝 Debug Q7 of 15
FastAPI - Dependency Injection
Identify the issue in the following FastAPI code snippet:
def fetch_value():
    return 10

@app.get('/value/')
def get_value(val: int = Depends(fetch_value())):
    return {"val": val}
AThe dependency function is called immediately instead of passing the function itself.
BThe route decorator is missing parentheses.
CThe return type of the dependency function is incorrect.
DThe endpoint path should not start with a slash.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the Depends usage

    Depends expects a callable (function), not the result of a function call.
  2. Step 2: Identify the mistake

    Using Depends(fetch_value()) calls the function immediately, passing its return value instead of the function.
  3. Step 3: Correct usage

    It should be Depends(fetch_value) without parentheses.
  4. Final Answer:

    The dependency function is called immediately instead of passing the function itself. -> Option A
  5. Quick Check:

    Depends requires a callable, not a function call [OK]
Quick Trick: Pass function to Depends, not function call [OK]
Common Mistakes:
MISTAKES
  • Calling the dependency function instead of passing it
  • Forgetting parentheses in route decorators
  • Misunderstanding dependency return types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes