Skip to content

fix: guard self.unit against None in Job.__repr__#653

Open
Jo2234 wants to merge 1 commit intodbader:masterfrom
Jo2234:fix/guard-unit-none-in-repr
Open

fix: guard self.unit against None in Job.__repr__#653
Jo2234 wants to merge 1 commit intodbader:masterfrom
Jo2234:fix/guard-unit-none-in-repr

Conversation

@Jo2234
Copy link
Copy Markdown

@Jo2234 Jo2234 commented Mar 1, 2026

Summary

Guard self.unit against None in Job.__repr__() to prevent a TypeError crash.

Problem

When self.unit is None (its initial value at line 233) and self.interval == 1, calling repr() or str() on the Job crashes:

>>> import schedule
>>> job = schedule.every(1)
>>> repr(job)
TypeError: 'NoneType' object is not subscriptable

This happens because self.unit[:-1] is called at lines 305 and 320 without first checking if self.unit is None.

Fix

-  self.unit[:-1] if self.interval == 1 else self.unit,
+  self.unit[:-1] if self.unit and self.interval == 1 else self.unit,

Applied to both occurrences (lines 305 and 320).

Fixes #646

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 dbader#646
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CRASH CONDITION] - Guard self.unit against None

1 participant