DEV CommunityWednesday · October 14, 2026FREE

Config precedence in Python: why your resolver shouldn't know what "CLI" means

pythonconfigurationarchitecturedevcommunity

The article opens with a familiar shape for configuration loading in Python: a function that inspects the environment, the command line, and a YAML file, then decides which source wins. The example checks `cli_args.timeout`, then `os.getenv("TIMEOUT")`, then falls back to `yaml_config.get("timeout", 30000)`. The author notes that repeating this pattern for every setting yields a function that is 200 lines long, impossible to test in isolation, and impossible to change precedence on without editing every branch. The version the author has been working on separates two concerns the if/else approach fuses: what order sources apply in, and how they merge. Once separated, the post says, precedence stops being code and starts being structure. The resolver does not know what "CLI" means. The article shows a `resolve_config` function that takes an iterable of `SourceSpec` objects, iterates over them, calls `spec.source.get_config()`, and on an exception checks `spec.failure_policy == FailurePolicy.REQUIRED` to decide whether to raise or log a warning. The excerpt cuts off mid-warning message, so the full merge behavior beyond that point is not described in the provided text.

// why it matters

The post argues that separating source order from merge logic makes Python config resolvers easier to test in isolation and easier to change precedence in.

Sources

Primary · DEV Community
▸ Read original at dev.to

Like this? Get the next digest.

Config precedence in Python: why your resolver shouldn't know what "CLI" means — aigest.dev