A feature derives its state from a source that does not exist, and reports the **default** instead of failing.
A feature derives its state from a source that does not exist, and reports the default instead of failing. Two shapes were reported in the same project, weeks apart:
In both cases there was no error to notice, the logs were clean, and the tests passed. The only symptom was a screen that never changed.
The code conflates two different facts:
Written as a one-liner, the decision wins silently:
state = read_source().get("state", "pending") # absence and "pending" become the same answerEvery layer that does this compounds it: a missing file yields {}, {} yields the default, the default yields "pending", and "pending" is a legitimate-looking state, so nothing downstream can tell that the input was never there. The failure is not that the default is wrong — it is that the reason for the default was never established.
The reason this keeps shipping is that the fallback is the convenient path: it makes the happy-path test pass, it makes an unpopulated environment "work", and it turns a loud missing-file error into a quiet empty result. It is exactly the trade that makes a feature impossible to debug later.
Make the absence visible at the boundary, and let only the layer that knows the semantics decide:
def load_state(path: Path) -> State:
if not path.exists():
raise SourceMissing(path) # absence is a fact, not a value
return parse(path.read_text(encoding="utf-8"))
def state_for(record, state: State) -> str:
# here absence has a *documented* meaning: this record never declared a state
return state.by_id.get(record.id, Decision.default_for_undeclared())Three rules that keep it honest:
If your platform makes absence an exception already (KeyError, FileNotFoundError), do not catch it just to return the default somewhere far from the place that knows why. Let it travel to the boundary that can act on it.
The check is a deletion, not a reading:
rm -rf <the source the feature derives from> # the file, the table, the key
<run the feature>
# PASS: it raises, or reports the absence, and says which source was missing
# FAIL: it reports "pending" (or any other legitimate-looking state) with no errorDo this once for each shape: a missing whole source, and a missing field inside a source that exists. The second is the one that hides longest, because the file is there and only the key is not.
.get(key, default) as a reflex; reach for it only after deciding what absence means..gitignore forgets to ship it — and the default is what hides it until production.When a feature reports a value that never changes, check whether its input is actually present before debugging the logic — and say which of the three (present / absent / unreadable) you observed. If you are the one writing the fallback, write down why absence equals that value, or make it an error. "It made the test pass" is the reason the bug exists.