Settings & Configuration — pydantic-settings and .env — Concept Check
Questions on Settings & Configuration — pydantic-settings and .env. BaseSettings reads typed config from environment variables and .env files — validated at startup, injected via a cached get_settings dependency, with secrets kept out of git entirely.
Questions in this quiz
8 questions on Settings & Configuration — pydantic-settings and .env — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer notices that their FastAPI application crashes at startup with a 'Field required' error for 'database_url'. What is the most likely cause?
- AThe .env file is not in the project directory.
- BThe app_name setting is not defined.
- CThe DATABASE_URL environment variable is missing.
- DThe JWT_SECRET is incorrectly set in the .env file.
2.In a production environment, where should sensitive information like JWT_SECRET be stored according to best practices?
- AIn a publicly accessible configuration file.
- BIn the source code as a constant.
- CIn the .env file committed to git.
- DIn environment variables managed by the platform.
3.A developer wants to test different configurations without modifying the environment variables. What approach should they take?
- AOverride the get_settings dependency in tests.
- BUse global variables to switch settings.
- CChange the environment variables directly in the test script.
- DModify the .env file for each test case.
4.Why is it recommended to use @lru_cache with the get_settings function in a FastAPI application?
- ATo ensure settings are loaded from .env on every request.
- BTo allow settings to be modified at runtime.
- CTo cache settings and avoid re-parsing on each request.
- DTo automatically refresh settings when the .env file changes.
5.A developer commits their .env file to git by mistake. What is the potential risk?
- AThe application will crash on startup.
- BThe application will use default settings instead.
- CThe .env file will override environment variables.
- DSensitive information might be exposed publicly.
6.What happens if both an environment variable and a .env file specify a value for the same setting?
- AThe value from the environment variable is used.
- BThe default value in the code is used.
- CThe value from the .env file is used.
- DThe application will throw a conflict error.
7.A developer sets DEBUG=true in the .env file but notices no change in the application's behavior. What could be the reason?
- AThe debug setting is not implemented in the code.
- BThe application requires a restart to apply .env changes.
- CThe .env file is not being read by the application.
- DThe environment variable DEBUG is set to false, overriding .env.
8.Why should a .env.example file be included in the git repository?
- ATo store actual secrets for production use.
- BTo ensure the application runs with default values.
- CTo automatically generate environment variables.
- DTo provide a template with dummy values for developers.