
A command-line program reads defaults and optional per-project overrides. The intended lookup order should be visible without copying every settings dictionary into a temporary object. The leftmost mapping wins a lookup. Converting to a dict here takes a snapshot for display, while a live ChainMap would refer to its component mappings.
Set up the smallest useful example
The script runs collections.ChainMap against three independent inputs. Each call starts from the value shown, so changing one example leaves the other expectations available for comparison. The table records real return values from the example. Quotation marks around text in the table make its boundaries visible.
from collections import ChainMap
def evaluate(value):
return dict(ChainMap(value,{'theme':'paper','size':14}))
for sample in [{'size': 16}, {'theme': 'ink', 'scale': 2}, {}]:
print(evaluate(sample))Inspect the boundary cases
| Input to evaluate | Observed result |
|---|---|
| {"size": 16} | {"theme": "paper", "size": 16} |
| {"theme": "ink", "scale": 2} | {"theme": "ink", "size": 14, "scale": 2} |
| {} | {"theme": "paper", "size": 14} |
For {"size": 16}, the observed output is {"theme": "paper", "size": 16}. With {"theme": "ink", "scale": 2}, it becomes {"theme": "ink", "size": 14, "scale": 2}. If a second implementation produces a different answer, begin with these small inputs. They allow a reviewer to trace the expression without reconstructing the entire application or replaying a production workload.
The input {} produces 2 named fields. Check the key as well as the associated value. A record containing a key with a false or empty value can carry a different instruction from an absent key.
Check the calling code
Writes to a ChainMap go to the first mapping. A deliberate settings editor should identify which file owns a value before persisting it.
A practical test can begin with assert evaluate({'size': 16}) == {'theme': 'paper', 'size': 16}. Add the other rows as separate expectations so one failure identifies the input that changed. For a service fed by stored data, repeat the check on one anonymized record from an earlier version of its file or message format.
One more fixture before integrating
Change one value in the project override layer after constructing the view and inspect the next lookup. This identifies whether the consumer expects a live layered view or a snapshot taken at configuration-load time.
The same Python function was also run with {} / {"size": 16, "extra": 1}. These additional inputs change the boundary without changing the implementation. An exception name in the table records where the operation rejects an input; a returned empty or zero value records a successful call. Choose deliberately which of those outcomes the surrounding feature should expose.
| Additional fixture | Result or exception |
|---|---|
| {} | {"theme": "paper", "size": 14} |
| {"size": 16, "extra": 1} | {"theme": "paper", "size": 16, "extra": 1} |
Python’s collections reference · Python standard library
Sources and further reading
Cover photograph: ThinkPad T430 (keyboard) by Vitaly Zdanevich. CC0. Cropped and typeset for this cover. The photograph illustrates the topic.
