refactor: 统一使用现代类型注解替代传统类型注解
- 将 Dict、List 等传统类型注解替换为 dict、list 等现代类型注解 - 更新类型注解以更精确地反映变量类型 - 修复部分类型注解与实际使用不匹配的问题 - 优化部分代码逻辑以提高类型安全性
This commit is contained in:
+1
-1
@@ -46,7 +46,7 @@ class SessionManager:
|
||||
"""Registry of active Claude Code project sessions with persistence and user isolation."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._sessions: Dict[str, Session] = {}
|
||||
self._sessions: dict[str, Session] = {}
|
||||
self._lock = asyncio.Lock()
|
||||
self._reaper_task: Optional[asyncio.Task] = None
|
||||
|
||||
|
||||
+2
-2
@@ -53,8 +53,8 @@ class Scheduler:
|
||||
"""Singleton that manages scheduled jobs with Feishu notifications."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._jobs: Dict[str, ScheduledJob] = {}
|
||||
self._tasks: Dict[str, asyncio.Task] = {}
|
||||
self._jobs: dict[str, ScheduledJob] = {}
|
||||
self._tasks: dict[str, asyncio.Task] = {}
|
||||
self._lock = asyncio.Lock()
|
||||
self._started = False
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import time
|
||||
import uuid
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Any, Callable, Dict, Optional
|
||||
from typing import Any, Awaitable, Callable, Dict, Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -43,17 +43,17 @@ class TaskRunner:
|
||||
"""Singleton that manages background tasks with Feishu notifications."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._tasks: Dict[str, BackgroundTask] = {}
|
||||
self._tasks: dict[str, BackgroundTask] = {}
|
||||
self._lock = asyncio.Lock()
|
||||
self._notification_handler: Optional[Callable] = None
|
||||
self._notification_handler: Optional[Callable[[BackgroundTask], Awaitable[None]]] = None
|
||||
|
||||
def set_notification_handler(self, handler: Optional[Callable]) -> None:
|
||||
def set_notification_handler(self, handler: Optional[Callable[[BackgroundTask], Awaitable[None]]]) -> None:
|
||||
"""Set custom notification handler for M3 mode (host client -> router)."""
|
||||
self._notification_handler = handler
|
||||
|
||||
async def submit(
|
||||
self,
|
||||
coro: Callable[[], Any],
|
||||
coro: Awaitable[Any],
|
||||
description: str,
|
||||
notify_chat_id: Optional[str] = None,
|
||||
user_id: Optional[str] = None,
|
||||
@@ -76,7 +76,7 @@ class TaskRunner:
|
||||
logger.info("Submitted background task %s: %s", task_id, description)
|
||||
return task_id
|
||||
|
||||
async def _run_task(self, task_id: str, coro: Callable[[], Any]) -> None:
|
||||
async def _run_task(self, task_id: str, coro: Awaitable[Any]) -> None:
|
||||
"""Execute a task and send notification on completion."""
|
||||
async with self._lock:
|
||||
task = self._tasks.get(task_id)
|
||||
@@ -130,14 +130,15 @@ class TaskRunner:
|
||||
msg += f"\n\n**Error:** {task.error}"
|
||||
|
||||
try:
|
||||
await send_text(task.notify_chat_id, "chat_id", msg)
|
||||
if task.notify_chat_id:
|
||||
await send_text(task.notify_chat_id, "chat_id", msg)
|
||||
except Exception:
|
||||
logger.exception("Failed to send notification for task %s", task.task_id)
|
||||
|
||||
def get_task(self, task_id: str) -> Optional[BackgroundTask]:
|
||||
return self._tasks.get(task_id)
|
||||
|
||||
def list_tasks(self, limit: int = 20) -> list[dict]:
|
||||
def list_tasks(self, limit: int = 20) -> list[dict[str, Any]]:
|
||||
tasks = sorted(
|
||||
self._tasks.values(),
|
||||
key=lambda t: t.started_at,
|
||||
|
||||
Reference in New Issue
Block a user