From e22d30fc7675668b968b3d54ab927a17734da382 Mon Sep 17 00:00:00 2001 From: Jo2234 <64789670+Jo2234@users.noreply.github.com> Date: Sun, 1 Mar 2026 15:17:59 +0800 Subject: [PATCH] fix: guard self.unit against None in Job.__repr__ When self.unit is None (before a time unit property like .seconds is accessed), calling repr() or str() on the Job crashes with: TypeError: 'NoneType' object is not subscriptable This happens because self.unit[:-1] is called unconditionally when self.interval == 1. The fix adds a truthiness check for self.unit before attempting the slice operation. Fixes #646 --- schedule/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/schedule/__init__.py b/schedule/__init__.py index 8e12eeb7..68c631a0 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -302,7 +302,7 @@ def is_repr(j): if self.at_time is not None: return "Every %s %s at %s do %s %s" % ( self.interval, - self.unit[:-1] if self.interval == 1 else self.unit, + self.unit[:-1] if self.unit and self.interval == 1 else self.unit, self.at_time, call_repr, timestats, @@ -317,7 +317,7 @@ def is_repr(j): return fmt % dict( interval=self.interval, latest=self.latest, - unit=(self.unit[:-1] if self.interval == 1 else self.unit), + unit=(self.unit[:-1] if self.unit and self.interval == 1 else self.unit), call_repr=call_repr, timestats=timestats, )