Share the 30-min latch cap check
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,free-days-interop,dependency-freshness) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / free-days-interop (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run

pylint duplicate-code: the evening and morning policies carried the same cap test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Krzysztof Rudnicki 2026-11-22 14:04:59 +01:00
parent 0d66b64fb5
commit 83e1e158fd
2 changed files with 10 additions and 13 deletions

View File

@ -24,8 +24,7 @@ from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING
from wake_alarm._constants import FALLBACK_RING_CAP
from wake_alarm._fallback_policy import CAP, Decision
from wake_alarm._fallback_policy import CAP, Decision, latch_capped
if TYPE_CHECKING:
from datetime import datetime
@ -65,13 +64,7 @@ def in_evening(now: datetime) -> bool:
def is_capped(facts: EveningFacts) -> bool:
"""Whether tonight's ring has used its 30 minutes."""
latch = facts.latch
if latch is None:
return False
if latch.stop_reason == CAP:
return True
started = latch.started()
return started is not None and facts.now >= started + FALLBACK_RING_CAP
return latch_capped(facts.latch, facts.now)
def needs_fetch(facts: EveningFacts) -> bool:

View File

@ -135,15 +135,19 @@ def needs_facts(now: datetime, alarm_at: datetime, latch: Latch | None) -> bool:
return alarm_at - FALLBACK_WINDOW_START <= now < alarm_at + BYPASS_WATCH
def is_capped(facts: Facts) -> bool:
"""Whether today's ring has used its 30 minutes."""
latch = facts.latch
def latch_capped(latch: Latch | None, now: datetime) -> bool:
"""Whether the ring this latch records has used its 30 minutes."""
if latch is None:
return False
if latch.stop_reason == CAP:
return True
started = latch.started()
return started is not None and facts.now >= started + FALLBACK_RING_CAP
return started is not None and now >= started + FALLBACK_RING_CAP
def is_capped(facts: Facts) -> bool:
"""Whether today's ring has used its 30 minutes."""
return latch_capped(facts.latch, facts.now)
def day_off(facts: Facts) -> str | None: