From 00c737becf27d0ce1fdeaaeda0afeef9b5c425ac Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 15 Dec 2025 20:51:19 +0000 Subject: [PATCH] Add guard against division by zero in target_shifts calculation with warning logging --- rota_generator/shifts.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/rota_generator/shifts.py b/rota_generator/shifts.py index 1633576..4d5c44c 100644 --- a/rota_generator/shifts.py +++ b/rota_generator/shifts.py @@ -2007,11 +2007,24 @@ class RotaBuilder(object): for i in shift.sites ) - target_shifts = ( - total_shifts - / full_time_equivalent_joined - * worker.get_fte(shift=shift.name) - ) + # Guard against division by zero: if no full-time + # equivalent is available for the shift's sites the + # denominator may be zero (for example when all site + # FTEs are 0). In that case emit a warning and set the + # target to 0 rather than raising an exception. + if not full_time_equivalent_joined: + # avoid ZeroDivisionError + self.add_warning( + "Zero FTE for sites", + f"full_time_equivalent sum is zero for shift {shift.name}; setting target_shifts=0 for worker {worker.name}", + ) + target_shifts = 0 + else: + target_shifts = ( + total_shifts + / full_time_equivalent_joined + * worker.get_fte(shift=shift.name) + ) if self.use_previous_shifts: if shift.name in worker.previous_shifts: