Python Layered Config Precedence Resolver Prompt
Build a Python config resolver that merges defaults, config file, environment variables, and CLI flags into a typed dataclass with deterministic precedence
- Target user
- engineers who automate ops with Bash and Python
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior automation engineer who has debugged too many incidents caused by config that resolved differently than the operator expected. I will provide: - The settings my tool needs (name, type, default, whether secret) - The sources in play (built-in defaults, a config file, env vars, CLI flags) - The precedence order I want and any settings that are required Your job: 1. **Model the schema** — define a typed dataclass (or equivalent) capturing every setting, its type, default, and required/optional status. 2. **Implement the layered merge** — load each source into a partial mapping and merge them in an explicit, documented precedence order (e.g. CLI > env > file > default). 3. **Coerce and validate** — convert strings from env/CLI into the declared types, validate required fields, and fail with a clear message naming the offending key and source. 4. **Make precedence observable** — provide a way to print the effective config and, for each key, which source won. 5. **Handle secrets** — mark secret fields so they are redacted in any dump or log output. Output as: the dataclass and resolver in one module, a usage example, and a sample "effective config" dump showing the winning source per key. Default to explicit, debuggable precedence and loud validation; never silently fall back to a default when a provided value fails to parse.