This FastAPI app uses a global dependency to check a header token on every request. If the token is wrong, it returns an error. Otherwise, it returns a list of items.
A. No output printed because common_dep is not global
B. Global dependency called printed once per request
C. Global dependency called printed twice per request
D. SyntaxError due to missing global dependency
Solution
Step 1: Check how common_dep is used
common_dep is used as a route dependency only on the read_item function, not as a global dependency.
Step 2: Understand when common_dep runs
It runs only when the /items/{item_id} route is called, printing the message once per request to that route.
Final Answer:
Global dependency called printed once per request -> Option B
Quick Check:
Route dependency prints only when route is called, not global [OK]
Hint: Route dependencies run per route, not global unless set globally [OK]
Common Mistakes:
Assuming common_dep is global when it's route-specific
Expecting multiple prints per request
Confusing global and route dependencies
4. Identify the error in this FastAPI app code that tries to use a global dependency:
from fastapi import FastAPI, Depends
def common_dep():
print("Running global dependency")
app = FastAPI(dependencies=Depends(common_dep))
medium
A. common_dep is not defined before use
B. Missing parentheses when calling FastAPI()
C. dependencies parameter expects a list, not a single Depends
D. Depends cannot be used as a global dependency
Solution
Step 1: Check the dependencies parameter type
The 'dependencies' argument expects a list of Depends instances, not a single Depends object.
Step 2: Identify the fix
Wrap Depends(common_dep) inside a list: dependencies=[Depends(common_dep)]
Final Answer:
dependencies parameter expects a list, not a single Depends -> Option C
Quick Check:
dependencies=[Depends(...)] requires a list [OK]
Hint: Always pass dependencies as a list, even if one item [OK]
Common Mistakes:
Passing Depends(...) directly without list
Defining dependency after app creation
Confusing Depends usage with function call
5. You want to add a global dependency that checks user authentication and also logs request info. Which is the best way to combine these in FastAPI?
hard
A. Add both auth check and logging as separate global dependencies in the dependencies list
B. Add auth check as global dependency and logging inside each route
C. Create one dependency function that calls both auth check and logging, then add it globally
D. Use middleware for auth and global dependency for logging
Solution
Step 1: Understand global dependencies list
FastAPI allows multiple global dependencies by passing a list to the dependencies parameter.
Step 2: Combine auth and logging as separate dependencies
Adding both as separate Depends in the list keeps concerns separated and runs both for every request.
Step 3: Evaluate other options
Create one dependency function that calls both auth check and logging, then add it globally mixes concerns in one function, B is inconsistent, D mixes middleware and dependencies unnecessarily.
Final Answer:
Add both auth check and logging as separate global dependencies in the dependencies list -> Option A
Quick Check:
Multiple global dependencies = dependencies=[Depends(...), Depends(...)] [OK]
Hint: Use a list of Depends for multiple global dependencies [OK]
Common Mistakes:
Combining unrelated logic in one dependency
Using middleware instead of dependencies for all tasks
Adding some logic only per route, not globally
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.