0
0
FastAPIframework~20 mins

Configuration management in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this FastAPI configuration code?
Consider this FastAPI app configuration using Pydantic BaseSettings. What will be the value of app.state.config.debug after running this code?
FastAPI
from fastapi import FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    debug: bool = False

app = FastAPI()

@app.on_event("startup")
async def startup_event():
    app.state.config = Settings(debug=True)

# After startup event, what is app.state.config.debug?
AFalse
BTrue
CNone
DRaises AttributeError
Attempts:
2 left
💡 Hint
Think about when the startup event runs and how the Settings instance is created.
📝 Syntax
intermediate
2:00remaining
Which option correctly loads environment variables into FastAPI settings?
Given this Pydantic settings class, which option correctly loads environment variables from a file named .env?
FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    app_name: str = "MyApp"
    debug: bool = False

# How to load .env file automatically?
A
class Settings(BaseSettings):
    def __init__(self):
        self.env_file = ".env"
B
class Settings(BaseSettings):
    env_file = ".env"
C
class Settings(BaseSettings):
    class Config:
        env_file = ".env"
D
class Settings(BaseSettings):
    env_path = ".env"
Attempts:
2 left
💡 Hint
Pydantic uses a nested Config class for settings like env_file.
state_output
advanced
2:00remaining
What is the value of config.app_name after this FastAPI app runs?
This FastAPI app uses a settings instance stored in app.state. What will config.app_name print?
FastAPI
from fastapi import FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    app_name: str = "DefaultApp"

app = FastAPI()

@app.on_event("startup")
async def startup():
    app.state.config = Settings(app_name="CustomApp")

config = app.state.config
print(config.app_name)
AAttributeError because app.state.config is not set yet
BCustomApp
CDefaultApp
DNone
Attempts:
2 left
💡 Hint
Consider when app.state.config is set versus when config is assigned.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI app fail to load environment variables?
This code tries to load environment variables but always uses default values. What is the cause?
FastAPI
from fastapi import FastAPI
from pydantic import BaseSettings

class Settings(BaseSettings):
    debug: bool = False

    class Config:
        env_file = ".env"

app = FastAPI()

@app.on_event("startup")
async def startup():
    app.state.config = Settings()

# .env file contains DEBUG=true
AThe startup event runs too late to load environment variables
BPydantic BaseSettings does not support env_file
CThe debug field must be uppercase to load from .env
DThe .env file is not in the current working directory when app runs
Attempts:
2 left
💡 Hint
Check where the app runs and where the .env file is located.
🧠 Conceptual
expert
3:00remaining
Which is the best practice for managing configuration in a FastAPI app for multiple environments?
You want to manage different settings for development, testing, and production in FastAPI. Which approach is best?
AUse a single Pydantic BaseSettings class with environment variables loaded from different .env files per environment
BUse global variables and change them manually before running the app
CHardcode all settings in the app code and comment/uncomment per environment
DCreate separate FastAPI apps for each environment with duplicated code
Attempts:
2 left
💡 Hint
Think about maintainability and automation for different environments.