From c6d39ace60ebe3f1d94fd31240090a09554b232a Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sat, 30 Aug 2025 20:46:03 +0100 Subject: [PATCH] Trip: fix total_distance undercount Previously Trip.total_distance returned None if any travel leg lacked a distance, which led to undercounted trip stats (less than flight + train totals). Now it accumulates only distances that are present and ignores missing/falsy values. The method returns a float and yields 0.0 when no distances are available. Docstring updated accordingly. Fixes #185 --- agenda/types.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/agenda/types.py b/agenda/types.py index 94830fd..d1ecdda 100644 --- a/agenda/types.py +++ b/agenda/types.py @@ -202,13 +202,18 @@ class Trip: """Countries flags for trip.""" return "".join(c.flag for c in self.countries) - def total_distance(self) -> float | None: - """Total distance for trip.""" - return ( - sum(t["distance"] for t in self.travel) - if all(t.get("distance") for t in self.travel) - else None - ) + def total_distance(self) -> float: + """Total distance for trip. + + Sums distances for travel items where a distance value is present. + Ignores legs with missing or falsy distances rather than returning None. + """ + total = 0.0 + for t in self.travel: + distance = t.get("distance") + if distance: + total += distance + return total def total_co2_kg(self) -> float | None: """Total CO₂ for trip."""