Skip to content

Make Timer.interval fully adjustable at runtime#82

Open
wbarnha wants to merge 1 commit into
masterfrom
claude/new-session-5qtat6
Open

Make Timer.interval fully adjustable at runtime#82
wbarnha wants to merge 1 commit into
masterfrom
claude/new-session-5qtat6

Conversation

@wbarnha

@wbarnha wbarnha commented Jul 23, 2026

Copy link
Copy Markdown
Member

Description

Timer.tick() and Timer.adjust_interval() already read self.interval_s
fresh on every iteration, so the nominal interval was effectively live. But
the values derived from the interval — the drift-correction bounds
min_interval_s / max_interval_s and the default max_drift threshold —
were computed once, in __init__. Reassigning the interval after the timer
started therefore left adjust_interval() clamping sleep times to the old
cadence, so the interval was not actually adjustable at runtime.

This promotes interval to a property whose setter recomputes all
interval-derived state through a shared _configure_interval() helper (also
used by __init__). Assigning timer.interval = X now retunes the timer
live — the next tick() uses the new interval, bounds, and default threshold.

Behavior

  • No change for existing callers: construction goes through the same
    _configure_interval() path and produces identical values.
  • timer.interval = X at runtime now updates interval_s, min_interval_s,
    max_interval_s, and the (interval-derived) default max_drift.
  • Because the default max_drift is interval-derived, changing the interval
    refreshes it; if you have pinned a custom max_drift, set it after changing
    the interval.

Tests

  • test_Timer_interval__setter_recomputes_derived_values — the setter
    refreshes interval_s, max_drift, and both correction bounds.
  • test_Timer_interval__small_interval_collapses_bounds — the
    sub-correction-window branch still collapses the bounds onto the interval.
  • test_Timer_interval__can_be_changed_at_runtime — a live interval change is
    honored by the iterator: the next sleep clamps to the new max_interval_s,
    not the old one (so the assertion only holds if the change took effect).

Full suite: 755 passed, 1 skipped locally (Python 3.11); ruff check and
ruff format --check clean.

Context

Follow-up to the drift-warning work in #78 (which makes max_drift
configurable) and issue #46 — this extends the same "retune a running timer"
idea to the interval itself. It is independent of #78; if both land there is a
trivial overlap in Timer.__init__.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Lt14zRDjGJv9YU9QZC3PqY


Generated by Claude Code

tick() and adjust_interval() already read self.interval_s fresh every
iteration, but the drift-correction bounds (min_interval_s /
max_interval_s) and the default max_drift threshold were derived from the
interval once, in __init__. Assigning a new interval mid-run therefore
left adjust_interval() clamping to the old cadence.

Promote interval to a property whose setter recomputes interval_s, the
default max_drift, and the correction bounds via a shared
_configure_interval() helper (also used by __init__). Assigning
timer.interval = X now retunes the timer live; the next tick uses the new
cadence. No behaviour change for existing callers -- construction goes
through the same path and yields identical values.

Add tests: the setter recomputes all derived values (including the
small-interval branch where the bounds collapse), and a runtime interval
change is honoured by the iterator (the next sleep clamps to the new
max_interval_s, not the old one).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lt14zRDjGJv9YU9QZC3PqY
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