Bird
0
0

How do you properly define a FastAPI dependency that takes a parameter and returns a callable dependency?

easy📝 Syntax Q3 of 15
FastAPI - Dependency Injection
How do you properly define a FastAPI dependency that takes a parameter and returns a callable dependency?
ADefine a function that returns another function which uses the parameter, then pass the outer function with the parameter to Depends.
BPass the parameter directly inside Depends without wrapping it in another function.
CUse a class with __call__ method but do not accept parameters in the constructor.
DDeclare the dependency function with default parameters and call it directly in the route.
Step-by-Step Solution
Solution:
  1. Step 1: Define outer function with parameter

    This function accepts the parameter you want to inject.
  2. Step 2: Define inner function as actual dependency

    This inner function uses the parameter and is returned by the outer function.
  3. Step 3: Use Depends with the outer function called with the parameter

    Pass Depends(get_dependency(param_value)) in the route.
  4. Final Answer:

    Define a function returning a function and pass the outer function with parameter to Depends. -> Option A
  5. Quick Check:

    Dependency with parameters requires a factory function [OK]
Quick Trick: Use a factory function returning a dependency function [OK]
Common Mistakes:
MISTAKES
  • Passing parameters directly inside Depends without wrapping
  • Not returning a callable from the outer function
  • Using classes without accepting parameters properly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes