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?
✗ 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?
✗ Incorrect
You use Depends() in the main dependency's parameters to include a sub-dependency.
If a sub-dependency raises an error, what happens?
✗ 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?
✗ Incorrect
Depends() is the FastAPI function used to declare dependencies and sub-dependencies.
What is a benefit of using sub-dependencies?
✗ 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.