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 shared dependency in FastAPI?
A shared dependency is a function or class that multiple path operations use to share common logic or data, like authentication or database sessions.
Click to reveal answer
beginner
How do you declare a shared dependency in FastAPI?
You declare a shared dependency by creating a function and then including it in multiple path operations or other dependencies using the Depends parameter.
Click to reveal answer
beginner
Why use shared dependencies in FastAPI?
Shared dependencies help avoid repeating code, keep logic centralized, and make your app easier to maintain and test.
Click to reveal answer
intermediate
What happens if a shared dependency raises an exception?
If a shared dependency raises an exception, FastAPI stops processing the request and returns the error response immediately.
Click to reveal answer
intermediate
Can shared dependencies depend on other dependencies in FastAPI?
Yes, shared dependencies can depend on other dependencies, allowing you to build complex dependency trees.
Click to reveal answer
What FastAPI feature allows you to reuse logic across multiple routes?
ABackground tasks
BMiddleware
CShared dependencies
DEvent handlers
✗ Incorrect
Shared dependencies let you reuse logic like authentication or database sessions across routes.
How do you include a shared dependency in a FastAPI path operation?
ABy adding it as a parameter with Depends()
BBy importing it in the route file
CBy calling it inside the route function
DBy registering it in middleware
✗ Incorrect
You include shared dependencies by adding them as parameters with Depends() in the route function.
If a shared dependency raises an HTTPException, what does FastAPI do?
ARetries the dependency
BReturns the error response immediately
CIgnores the error and continues
DLogs the error but returns success
✗ Incorrect
FastAPI stops processing and returns the error response immediately.
Can shared dependencies depend on other dependencies in FastAPI?
AYes, dependencies can be nested
BNo, dependencies must be independent
COnly if they are classes
DOnly if they are async functions
✗ Incorrect
FastAPI supports nested dependencies to build complex logic.
What is a benefit of using shared dependencies?
APrevents error handling
BMakes the app slower
CRequires more code duplication
DAvoids repeating code and centralizes logic
✗ Incorrect
Shared dependencies help avoid code repetition and centralize logic.
Explain how shared dependencies work in FastAPI and why they are useful.
Think about how you avoid repeating code in multiple routes.
You got /5 concepts.
Describe how you can handle errors raised in shared dependencies in FastAPI.
Consider what happens if a dependency fails.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using Depends() in FastAPI for shared dependencies?
easy
A. To reuse code across multiple routes by declaring common dependencies
B. To create a new route in the application
C. To handle database connections manually
D. To define the response model for an endpoint
Solution
Step 1: Understand the role of Depends()
Depends() is used in FastAPI to declare dependencies that can be shared across multiple routes.
Step 2: Identify the main benefit
Using shared dependencies helps reuse code and keep the app clean and maintainable.
Final Answer:
To reuse code across multiple routes by declaring common dependencies -> Option A
Quick Check:
Shared dependencies = reuse code [OK]
Hint: Depends() means shared code for many routes [OK]
The function common_dep returns the string "shared value". FastAPI injects this into the value parameter via Depends(common_dep).
Step 2: Check the returned JSON
The route returns a dictionary with item_id from the path and value from the dependency. So the output will be {"item_id": 42, "value": "shared value"}.
Final Answer:
{"item_id": 42, "value": "shared value"} -> Option D
Quick Check:
Dependency injects "shared value" [OK]
Hint: Depends injects return value as parameter [OK]
Common Mistakes:
Assuming dependency returns item_id
Expecting error due to missing parameter
Thinking value will be null without explicit call
4. Identify the error in the following FastAPI code using shared dependencies:
A. The function secure_route should not have parameters
B. The dependency function get_token() should return an int
C. Depends() is missing the dependency function inside
D. The route decorator is missing parentheses
Solution
Step 1: Check Depends usage
The parameter token uses Depends() without specifying the dependency function. This is incorrect syntax.
Step 2: Correct usage
Depends must wrap the function providing the dependency, so it should be Depends(get_token).
Final Answer:
Depends() is missing the dependency function inside -> Option C
Quick Check:
Depends() needs function argument [OK]
Hint: Depends() always needs a function inside [OK]
Common Mistakes:
Using Depends() without argument
Expecting Depends() to work without function
Confusing Depends() with decorator syntax
5. You want to share a database session dependency across multiple routes but also ensure it closes after each request. Which approach correctly uses shared dependencies with cleanup in FastAPI?
hard
A. Create the session globally once and reuse it without closing
B. Use a dependency function with yield that creates the session, yields it, then closes it after
C. Pass the session as a normal parameter without Depends()
D. Use Depends() but close the session manually inside each route
Solution
Step 1: Understand dependency cleanup
FastAPI supports dependencies with cleanup by using yield inside the dependency function. This allows setup before yield and cleanup after.
Step 2: Apply to database session
The correct pattern is to create the session, yield it for use in routes, then close it after the request finishes.
Final Answer:
Use a dependency function with yield that creates the session, yields it, then closes it after -> Option B
Quick Check:
Yield in dependency = setup and cleanup [OK]
Hint: Use yield in dependency for setup and cleanup [OK]
Common Mistakes:
Reusing global session without closing
Closing session inside route instead of dependency