Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a sub-dependency in FastAPI?
A sub-dependency is a dependency that is used inside another dependency. It helps break down complex dependencies into smaller, reusable parts.
Click to reveal answer
beginner
How do you declare a sub-dependency in FastAPI?
You declare a sub-dependency by adding a dependency parameter inside another dependency function using Depends().
Click to reveal answer
intermediate
Why use sub-dependencies in FastAPI?
Sub-dependencies help organize code better, avoid repetition, and make testing easier by isolating smaller parts of logic.
Click to reveal answer
intermediate
Example: What does this code do?
```python
from fastapi import Depends
def get_token_header():
return "token"
def get_current_user(token: str = Depends(get_token_header)):
return f"User with {token}"
```
The function get_current_user depends on get_token_header. FastAPI will first run get_token_header to get the token, then pass it to get_current_user.
Click to reveal answer
intermediate
How does FastAPI handle errors in sub-dependencies?
If a sub-dependency raises an error, FastAPI stops and returns that error response immediately without running the main dependency or endpoint.
Click to reveal answer
What is the purpose of sub-dependencies in FastAPI?
ATo replace middleware
BTo make the app slower
CTo break down dependencies into smaller reusable parts
DTo avoid using Depends()
✗ Incorrect
Sub-dependencies help organize code by breaking dependencies into smaller parts that can be reused.
How do you include a sub-dependency inside another dependency?
ABy using Depends() inside the main dependency's parameters
BBy calling the sub-dependency function directly
CBy importing the sub-dependency globally
DBy using async/await keywords
✗ Incorrect
You use Depends() in the main dependency's parameters to include a sub-dependency.
If a sub-dependency raises an error, what happens?
AFastAPI retries the sub-dependency
BFastAPI stops and returns the error response immediately
CFastAPI ignores the error and continues
DFastAPI logs the error but runs the endpoint
✗ Incorrect
FastAPI stops processing and returns the error response immediately if a sub-dependency fails.
Which FastAPI function is used to declare dependencies and sub-dependencies?
AInject()
BDependsOn()
CRequire()
DDepends()
✗ Incorrect
Depends() is the FastAPI function used to declare dependencies and sub-dependencies.
What is a benefit of using sub-dependencies?
AAvoids code repetition and improves testing
BMakes code harder to read
CSlows down the app
DRemoves the need for routers
✗ Incorrect
Sub-dependencies help avoid repeating code and make testing smaller parts easier.
Explain how sub-dependencies work in FastAPI and why they are useful.
Think about how smaller parts can be combined to build bigger features.
You got /5 concepts.
Describe what happens when a sub-dependency raises an error in FastAPI.
Consider how FastAPI handles errors to keep the app safe and predictable.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of sub-dependencies in FastAPI?
easy
A. To handle HTTP requests directly
B. To create database connections only
C. To reuse small parts of code inside other dependencies
D. To replace middleware functions
Solution
Step 1: Understand what sub-dependencies do
Sub-dependencies allow you to reuse small parts of code inside other dependencies, making your code cleaner.
Step 2: Compare options with this purpose
Only To reuse small parts of code inside other dependencies correctly describes this purpose; others describe unrelated tasks.
Final Answer:
To reuse small parts of code inside other dependencies -> Option C
Quick Check:
Sub-dependencies = Reuse code [OK]
Hint: Sub-dependencies help reuse code inside dependencies [OK]
Assuming sub-dependencies only manage database connections
2. Which of the following is the correct way to declare a sub-dependency in FastAPI?
easy
A. def sub_dep(): return 'data'
def main_dep(sub: str = Depends(sub_dep)): return sub
B. def sub_dep(): return 'data'
def main_dep(sub: str = sub_dep()): return sub
C. def sub_dep(): return 'data'
def main_dep(sub: str = Depends(sub_dep())): return sub
D. def sub_dep(): return 'data'
def main_dep(sub: str): return sub
Solution
Step 1: Recall FastAPI dependency syntax
Dependencies must be passed as functions inside Depends(), not called directly.
Step 2: Analyze each option
def sub_dep(): return 'data'
def main_dep(sub: str = Depends(sub_dep)): return sub correctly uses Depends(sub_dep) without calling it. Options B and C call the function, which is incorrect. def sub_dep(): return 'data'
def main_dep(sub: str): return sub lacks Depends entirely.
Final Answer:
def sub_dep(): return 'data'\n\ndef main_dep(sub: str = Depends(sub_dep)): return sub -> Option A
Quick Check:
Use Depends(function) without parentheses [OK]
Hint: Use Depends with function name, no parentheses [OK]
Common Mistakes:
Calling the dependency function inside Depends()
Not using Depends at all
Passing the function call result instead of the function
3. Given the code below, what will be the output when calling /items/42?
A. Calling main_dep() inside Depends instead of passing the function
B. Missing return statement in sub_dep
C. Incorrect route path syntax
D. Using async def for endpoint without await
Solution
Step 1: Identify how Depends should be used
Depends expects a function reference, not a function call. Calling main_dep() executes it immediately, which is wrong.
Step 2: Check the code for this mistake
The code uses Depends(main_dep()), which calls the function instead of passing it. It should be Depends(main_dep).
Final Answer:
Calling main_dep() inside Depends instead of passing the function -> Option A
Quick Check:
Depends needs function, not function call [OK]
Hint: Pass function to Depends, don't call it [OK]
Common Mistakes:
Calling dependency functions inside Depends()
Confusing async usage with dependency errors
Ignoring Depends syntax rules
5. You want to create a FastAPI endpoint that depends on a main dependency which itself depends on two sub-dependencies. How should you structure the dependencies to ensure both sub-dependencies are called and their results used in the main dependency?
hard
A. Call both sub-dependencies inside main dependency without Depends()
B. Pass sub-dependencies as global variables to main dependency
C. Use a single sub-dependency that returns a tuple of both results
D. Define two sub-dependency functions, then in main dependency use Depends() for both as parameters
Solution
Step 1: Understand sub-dependency usage in main dependency
FastAPI allows multiple dependencies by declaring parameters with Depends(). To use two sub-dependencies, declare both as parameters with Depends().
Step 2: Evaluate options for correctness
Define two sub-dependency functions, then in main dependency use Depends() for both as parameters correctly describes this pattern. Call both sub-dependencies inside main dependency without Depends() misses Depends(), so sub-dependencies won't be injected. Use a single sub-dependency that returns a tuple of both results is possible but less clear and not standard. Pass sub-dependencies as global variables to main dependency is incorrect as global variables don't work for dependency injection.
Final Answer:
Define two sub-dependency functions, then in main dependency use Depends() for both as parameters -> Option D