refactor: enforce BasedPyright strict type checking (#5158)
This commit is contained in:
1 parent
e703481755
commit
757ad9c764
166 files changed
+4728
-2621
No files matched your search
@@ -1,13 +1,18 @@
|
||||
"""Cron service for scheduled agent tasks."""
|
||||
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from nanobot.cron.types import CronJob, CronSchedule
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nanobot.cron.service import CronService
|
||||
|
||||
__all__ = ["CronService", "CronJob", "CronSchedule"]
|
||||
|
||||
_LAZY = {"CronService": ".service"}
|
||||
|
||||
|
||||
def __getattr__(name: str):
|
||||
def __getattr__(name: str) -> Any:
|
||||
module_path = _LAZY.get(name)
|
||||
if module_path is None:
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
@@ -6,7 +6,7 @@ import asyncio
|
||||
import hashlib
|
||||
import time
|
||||
import uuid
|
||||
from typing import Any, Protocol
|
||||
from typing import TYPE_CHECKING, Any, Protocol
|
||||
|
||||
from nanobot.agent.tools.cron import CronTool
|
||||
from nanobot.bus.events import InboundMessage, OutboundMessage
|
||||
@@ -16,9 +16,12 @@ from nanobot.cron.types import CronJob
|
||||
from nanobot.cron.webui_metadata import cron_proactive_delivery_metadata
|
||||
from nanobot.utils.prompt_templates import render_template
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nanobot.agent.tools.registry import ToolRegistry
|
||||
|
||||
|
||||
class BoundCronAgent(Protocol):
|
||||
tools: Any
|
||||
tools: ToolRegistry
|
||||
|
||||
async def submit_cron_turn(self, msg: InboundMessage) -> OutboundMessage | None:
|
||||
...
|
||||
|
||||
+23
-15
@@ -10,6 +10,7 @@ from contextlib import suppress
|
||||
from dataclasses import asdict
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from types import EllipsisType
|
||||
from typing import Any, Callable, Coroutine, Literal
|
||||
|
||||
from filelock import FileLock
|
||||
@@ -160,7 +161,7 @@ class CronService:
|
||||
self._lock = FileLock(str(self._action_path.parent) + ".lock")
|
||||
self.on_job = on_job
|
||||
self._store: CronStore | None = None
|
||||
self._timer_task: asyncio.Task | None = None
|
||||
self._timer_task: asyncio.Task[None] | None = None
|
||||
self._running = False
|
||||
self._timer_active = False
|
||||
self.max_sleep_ms = max_sleep_ms
|
||||
@@ -243,19 +244,21 @@ class CronService:
|
||||
return None
|
||||
return jobs, version
|
||||
|
||||
def _merge_action(self):
|
||||
def _merge_action(self) -> None:
|
||||
if not self._action_path.exists():
|
||||
return
|
||||
|
||||
jobs_map = {j.id: j for j in self._store.jobs}
|
||||
def _update(params: dict):
|
||||
jobs_map = {job.id: job for job in self._store.jobs} # pyright: ignore[reportOptionalMemberAccess]
|
||||
|
||||
def _update(params: dict[str, Any]) -> None:
|
||||
j = CronJob.from_dict(params)
|
||||
_normalize_agent_turn_job(j)
|
||||
jobs_map[j.id] = j
|
||||
|
||||
def _del(params: dict):
|
||||
if job_id := params.get("job_id"):
|
||||
jobs_map.pop(job_id)
|
||||
def _del(params: dict[str, Any]) -> None:
|
||||
job_id = params.get("job_id")
|
||||
if isinstance(job_id, str) and job_id:
|
||||
jobs_map.pop(job_id, None)
|
||||
|
||||
with self._lock:
|
||||
with open(self._action_path, "r", encoding="utf-8") as f:
|
||||
@@ -274,7 +277,7 @@ class CronService:
|
||||
except Exception:
|
||||
logger.exception("load action line error")
|
||||
continue
|
||||
self._store.jobs = list(jobs_map.values())
|
||||
self._store.jobs = list(jobs_map.values()) # pyright: ignore[reportOptionalMemberAccess]
|
||||
if self._running and changed:
|
||||
self._action_path.write_text("", encoding="utf-8")
|
||||
self._save_store()
|
||||
@@ -569,7 +572,8 @@ class CronService:
|
||||
# Handle one-shot jobs
|
||||
if job.schedule.kind == "at":
|
||||
if job.delete_after_run:
|
||||
self._store.jobs = [j for j in self._store.jobs if j.id != job.id]
|
||||
store = self._require_store()
|
||||
store.jobs = [item for item in store.jobs if item.id != job.id]
|
||||
else:
|
||||
job.enabled = False
|
||||
job.state.next_run_at_ms = None
|
||||
@@ -577,7 +581,11 @@ class CronService:
|
||||
# Compute next run
|
||||
job.state.next_run_at_ms = _compute_next_run(job.schedule, _now_ms())
|
||||
|
||||
def _append_action(self, action: Literal["add", "del", "update"], params: dict):
|
||||
def _append_action(
|
||||
self,
|
||||
action: Literal["add", "del", "update"],
|
||||
params: dict[str, Any],
|
||||
) -> None:
|
||||
self.store_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with self._lock:
|
||||
with open(self._action_path, "a", encoding="utf-8") as f:
|
||||
@@ -615,11 +623,11 @@ class CronService:
|
||||
channel: str | None = None,
|
||||
to: str | None = None,
|
||||
delete_after_run: bool = False,
|
||||
channel_meta: dict | None = None,
|
||||
channel_meta: dict[str, Any] | None = None,
|
||||
session_key: str | None = None,
|
||||
origin_channel: str | None = None,
|
||||
origin_chat_id: str | None = None,
|
||||
origin_metadata: dict | None = None,
|
||||
origin_metadata: dict[str, Any] | None = None,
|
||||
) -> CronJob:
|
||||
"""Add a new job."""
|
||||
_validate_schedule_for_add(schedule)
|
||||
@@ -727,8 +735,8 @@ class CronService:
|
||||
schedule: CronSchedule | None = None,
|
||||
message: str | None = None,
|
||||
deliver: bool | None = None,
|
||||
channel: str | None = ...,
|
||||
to: str | None = ...,
|
||||
channel: str | None | EllipsisType = ...,
|
||||
to: str | None | EllipsisType = ...,
|
||||
delete_after_run: bool | None = None,
|
||||
) -> CronJob | Literal["not_found", "protected"]:
|
||||
"""Update mutable fields of an existing job. System jobs cannot be updated.
|
||||
@@ -804,7 +812,7 @@ class CronService:
|
||||
store = self._require_store()
|
||||
return next((j for j in store.jobs if j.id == job_id), None)
|
||||
|
||||
def status(self) -> dict:
|
||||
def status(self) -> dict[str, object]:
|
||||
"""Get service status."""
|
||||
store = self._require_store()
|
||||
return {
|
||||
|
||||
+25
-10
@@ -3,11 +3,19 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Literal
|
||||
from typing import Any, Literal, cast, overload
|
||||
|
||||
from nanobot.utils.dict_keys import get_camel_snake
|
||||
|
||||
|
||||
@overload
|
||||
def _store_int(value: Any, default: Literal[None]) -> int | None: ...
|
||||
|
||||
|
||||
@overload
|
||||
def _store_int(value: Any, default: int = 0) -> int: ...
|
||||
|
||||
|
||||
def _store_int(value: Any, default: int | None = 0) -> int | None:
|
||||
"""Coerce JSON numerics to int; treat null/blank like a missing key."""
|
||||
if value is None or value == "":
|
||||
@@ -103,7 +111,10 @@ class CronJobState:
|
||||
|
||||
@classmethod
|
||||
def from_store_dict(cls, data: dict[str, Any]) -> CronJobState:
|
||||
history = get_camel_snake(data, "runHistory", "run_history", []) or []
|
||||
history = cast(
|
||||
list[object],
|
||||
get_camel_snake(data, "runHistory", "run_history", []) or [],
|
||||
)
|
||||
return cls(
|
||||
next_run_at_ms=_store_int(
|
||||
get_camel_snake(data, "nextRunAtMs", "next_run_at_ms"), None
|
||||
@@ -116,7 +127,7 @@ class CronJobState:
|
||||
run_history=[
|
||||
record
|
||||
if isinstance(record, CronRunRecord)
|
||||
else CronRunRecord.from_store_dict(record)
|
||||
else CronRunRecord.from_store_dict(cast(dict[str, Any], record))
|
||||
for record in history
|
||||
if isinstance(record, (dict, CronRunRecord))
|
||||
],
|
||||
@@ -137,16 +148,20 @@ class CronJob:
|
||||
delete_after_run: bool = False
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, kwargs: dict):
|
||||
state_kwargs = dict(kwargs.get("state", {}))
|
||||
def from_dict(cls, kwargs: dict[str, Any]) -> CronJob:
|
||||
state_kwargs = dict(cast(dict[str, Any], kwargs.get("state", {})))
|
||||
state_kwargs["run_history"] = [
|
||||
record if isinstance(record, CronRunRecord) else CronRunRecord(**record)
|
||||
for record in state_kwargs.get("run_history", [])
|
||||
record
|
||||
if isinstance(record, CronRunRecord)
|
||||
else CronRunRecord(**cast(dict[str, Any], record))
|
||||
for record in cast(list[object], state_kwargs.get("run_history", []))
|
||||
]
|
||||
kwargs["schedule"] = CronSchedule(**kwargs.get("schedule", {"kind": "every"}))
|
||||
kwargs["payload"] = CronPayload(**kwargs.get("payload", {}))
|
||||
kwargs["schedule"] = CronSchedule(
|
||||
**cast(dict[str, Any], kwargs.get("schedule", {"kind": "every"}))
|
||||
)
|
||||
kwargs["payload"] = CronPayload(**cast(dict[str, Any], kwargs.get("payload", {})))
|
||||
kwargs["state"] = CronJobState(**state_kwargs)
|
||||
return cls(**kwargs)
|
||||
return cls(**cast(Any, kwargs))
|
||||
|
||||
@classmethod
|
||||
def from_store_dict(cls, data: dict[str, Any]) -> CronJob:
|
||||
|
||||
Reference in new issue
Block a user