0
0
FastAPIframework~20 mins

Why dependency injection matters in FastAPI - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dependency Injection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use dependency injection in FastAPI?

What is the main benefit of using dependency injection in FastAPI applications?

AIt allows automatic management and sharing of components like database connections, making code easier to test and maintain.
BIt forces all functions to be asynchronous, improving performance automatically.
CIt removes the need to write any function parameters, simplifying code drastically.
DIt automatically generates HTML templates without extra code.
Attempts:
2 left
💡 Hint

Think about how dependency injection helps with reusing and testing parts of your app.

component_behavior
intermediate
2:00remaining
Effect of dependency injection on endpoint behavior

Given this FastAPI endpoint using dependency injection, what will be the output when accessed?

from fastapi import FastAPI, Depends

app = FastAPI()

def get_number():
    return 42

@app.get("/number")
async def read_number(num: int = Depends(get_number)):
    return {"number": num}
A404 Not Found error
B{"number": "num"}
CTypeError because Depends is used incorrectly
D{"number": 42}
Attempts:
2 left
💡 Hint

Consider what the Depends function does to the parameter num.

lifecycle
advanced
2:00remaining
When are dependencies called in FastAPI?

In FastAPI, when does a dependency function get executed during a request?

ABefore the endpoint function is called, so its return value can be passed as a parameter.
BAfter the endpoint function returns the response.
COnly once when the app starts, then reused for all requests.
DOnly if the endpoint raises an exception.
Attempts:
2 left
💡 Hint

Think about when the endpoint needs the data from the dependency.

📝 Syntax
advanced
2:00remaining
Identify the correct dependency injection syntax

Which option correctly uses dependency injection to provide a database session to a FastAPI endpoint?

FastAPI
from fastapi import Depends

def get_db():
    db = "db_session"
    try:
        yield db
    finally:
        pass  # close db

@app.get("/items")
async def read_items(db = ???):
    return {"db": db}
Aget_db
BDepends(get_db)
CDepends(get_db())
Dget_db()
Attempts:
2 left
💡 Hint

Remember that Depends expects a function, not a function call.

🔧 Debug
expert
2:00remaining
Why does this FastAPI dependency cause an error?

Consider this code snippet:

from fastapi import FastAPI, Depends

app = FastAPI()

def get_value():
    return 10

@app.get("/value")
async def read_value(val: int = Depends(get_value())):
    return {"val": val}

What error will this code cause when starting the app?

ASyntaxError due to missing colon
BRuntimeError because get_value returns None
CTypeError because Depends expects a callable, but got an int
DNo error, it works fine
Attempts:
2 left
💡 Hint

Look carefully at how Depends is used with parentheses.