Skip to content

Fix Job.__repr__ crash on a partially-composed default-interval job - #662

Closed
afonsojanu wants to merge 1 commit into
dbader:masterfrom
afonsojanu:fix/repr-crash-default-interval-none-unit
Closed

afonsojanu wants to merge 1 commit into
dbader:masterfrom
afonsojanu:fix/repr-crash-default-interval-none-unit

Conversation

@afonsojanu

Copy link
Copy Markdown

Fixes the crash reported in #646.

Job.__repr__ tries to drop the trailing s from the unit name for singular jobs (self.unit[:-1] if self.interval == 1 else self.unit), but a Job doesn't get a unit until one of the time-unit properties (.minute, .day, ...) or .do() sets it. Since every() defaults interval to 1, repr(schedule.every()) slices None and blows up with TypeError: 'NoneType' object is not subscriptable. This can bite you in an interactive session or any debug logging that reprs a job before it's fully composed.

There's actually already a test for exactly this "partially-composed job" scenario (test_to_repr's schedule.every(10) case), but it only happens to dodge the bug because interval=10 takes the other branch of that conditional and never slices anything.

Fix: route both places that do this slicing through a small format_unit helper that passes None straight through, and add a case to that same test for the default interval of 1, which is the one that actually crashed.

>>> import schedule
>>> repr(schedule.every())
Traceback (most recent call last):
  ...
TypeError: 'NoneType' object is not subscriptable

now returns 'Every 1 None do [None] (last run: [never], next run: [never])' instead, matching what the interval>1 case already does.

repr() slices self.unit to drop the trailing 's' for singular units,
but a freshly created Job (e.g. schedule.every(), interval defaults to
1) has unit=None until a time-unit property or do() sets it, so that
slice raised "TypeError: 'NoneType' object is not subscriptable".
There was already a test guarding the interval>1 case for exactly this
partially-composed state, it just never exercised interval==1.

Route both repr branches through a small helper that leaves None
untouched, and add a case for the default interval to the existing
test.
@afonsojanu afonsojanu closed this Oct 5, 2026
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.

1 participant