Skip to content
Sarj Code Standards

no-failure-conflated-with-absence

Distinguish a caught failure from a missing result.

Why

Callers cannot distinguish an operation that failed from a successful lookup with no result when both paths return None.

Fix

Let the failure propagate, or return a typed outcome with separate variants for a missing result and a failed operation.

Examples

Before — flagged A failed read looks like a missing score
app/scores.py
from pathlib import Path
def read_score(path: Path, enabled: bool) -> int | None:
if not enabled:
return None
try:
return int(path.read_text())
except (OSError, ValueError):
return None
After — preferred Missing scores and failed reads have distinct outcomes
app/scores.py
from pathlib import Path
from app.outcomes import MissingScore, ReadFailure, Score
def read_score(path: Path, enabled: bool) -> Score | MissingScore | ReadFailure:
if not enabled:
return MissingScore()
try:
return Score(int(path.read_text()))
except (OSError, ValueError) as error:
return ReadFailure(error)