Skip to content

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

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

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

Conversation

@juliosuas
Copy link
Copy Markdown

Summary

Fixes #646.

When Job.interval == 1 and self.unit is None (the initial value before a unit is set), __repr__ attempted self.unit[:-1], which raised:

TypeError: 'NoneType' object is not subscriptable

Root Cause

self.unit is initialized to None in Job.__init__:

self.unit: Optional[str] = None

But both format paths in __repr__ unconditionally subscript self.unit:

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

Fix

Guard both paths with a None check, substituting '[unit not set]' as a safe placeholder:

(self.unit[:-1] if self.interval == 1 else self.unit) if self.unit else '[unit not set]'

Verification

job = schedule.Job(interval=1)
repr(job)  # Before: TypeError crash / After: 'Every 1 [unit not set] do [None] ...'

When Job.interval == 1 and self.unit is None (the initial state),
__repr__ attempted self.unit[:-1] which raised:
  TypeError: 'NoneType' object is not subscriptable

Guard both format paths in __repr__ against self.unit being None by
substituting '[unit not set]' as a safe placeholder.

Fixes dbader#646
@juliosuas
Copy link
Copy Markdown
Author

Quick note: the fix also handles the else-branch in __repr__ where the same self.unit[:-1] pattern appeared in the format string dict. Both paths now safely fall back to '[unit not set]' when self.unit is None. Normal jobs that have been assigned a unit are completely unaffected.

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