Bird
0
0

Which of the following is the correct way to declare a dependency in a FastAPI path operation function?

easy📝 Syntax Q12 of 15
FastAPI - Dependency Injection
Which of the following is the correct way to declare a dependency in a FastAPI path operation function?
Adef read_item(item_id: int, user=Depends[get_current_user]):
Bdef read_item(item_id: int, user=Depends):
Cdef read_item(item_id: int, user=Depends(get_current_user)):
Ddef read_item(item_id: int, user=get_current_user()):
Step-by-Step Solution
Solution:
  1. Step 1: Recall the syntax for dependencies

    Dependencies are declared by assigning a parameter to Depends() with the dependency function inside.

  2. Step 2: Check each option

    def read_item(item_id: int, user=Depends(get_current_user)): correctly uses user=Depends(get_current_user). Others have syntax errors or call the function directly.

  3. Final Answer:

    def read_item(item_id: int, user=Depends(get_current_user)): -> Option C
  4. Quick Check:

    Depends() with function inside parentheses [OK]
Quick Trick: Use Depends(function_name) with parentheses [OK]
Common Mistakes:
MISTAKES
  • Calling the dependency function directly
  • Using Depends without parentheses
  • Using square brackets instead of parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes