Skip to content

Commit c5109d0

Browse files
committed
fix: resolve ruff violations without new ignores
1 parent e118878 commit c5109d0

56 files changed

Lines changed: 210 additions & 248 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

astrbot/cli/utils/plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import shutil
22
import tempfile
3-
from enum import Enum
3+
from enum import StrEnum
44
from io import BytesIO
55
from pathlib import Path
66
from zipfile import ZipFile
@@ -12,7 +12,7 @@
1212
from .version_comparator import VersionComparator
1313

1414

15-
class PluginStatus(str, Enum):
15+
class PluginStatus(StrEnum):
1616
INSTALLED = "installed"
1717
NEED_UPDATE = "needs-update"
1818
NOT_INSTALLED = "not-installed"

astrbot/core/agent/agent.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from dataclasses import dataclass
2-
from typing import Any, Generic
2+
from typing import Any
33

44
from .hooks import BaseAgentRunHooks
5-
from .run_context import TContext
65
from .tool import FunctionTool
76

87

98
@dataclass
10-
class Agent(Generic[TContext]):
9+
class Agent[TContext]:
1110
name: str
1211
instructions: str | None = None
1312
tools: list[str | FunctionTool] | None = None

astrbot/core/agent/handoff.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
from typing import Generic
2-
31
from .agent import Agent
4-
from .run_context import TContext
52
from .tool import FunctionTool
63

74

8-
class HandoffTool(FunctionTool, Generic[TContext]):
5+
class HandoffTool[TContext](FunctionTool):
96
"""Handoff tool for delegating tasks to another agent."""
107

118
def __init__(

astrbot/core/agent/hooks.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
from typing import Generic
2-
31
import mcp
42

53
from astrbot.core.agent.tool import FunctionTool
64
from astrbot.core.provider.entities import LLMResponse
75

8-
from .run_context import ContextWrapper, TContext
6+
from .run_context import ContextWrapper
97

108

11-
class BaseAgentRunHooks(Generic[TContext]):
9+
class BaseAgentRunHooks[TContext]:
1210
async def on_agent_begin(self, run_context: ContextWrapper[TContext]) -> None: ...
1311
async def on_tool_start(
1412
self,

astrbot/core/agent/mcp_client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import logging
33
from contextlib import AsyncExitStack
44
from datetime import timedelta
5-
from typing import Generic
65

76
from tenacity import (
87
before_sleep_log,
@@ -16,7 +15,6 @@
1615
from astrbot.core.agent.run_context import ContextWrapper
1716
from astrbot.core.utils.log_pipe import LogPipe
1817

19-
from .run_context import TContext
2018
from .tool import FunctionTool
2119

2220
try:
@@ -101,7 +99,7 @@ async def _quick_test_mcp_connection(config: dict) -> tuple[bool, str]:
10199
return True, ""
102100
return False, f"HTTP {response.status}: {response.reason}"
103101

104-
except asyncio.TimeoutError:
102+
except TimeoutError:
105103
return False, f"Connection timeout: {timeout} seconds"
106104
except Exception as e:
107105
return False, f"{e!s}"
@@ -360,7 +358,7 @@ async def cleanup(self) -> None:
360358
self.running_event.set()
361359

362360

363-
class MCPTool(FunctionTool, Generic[TContext]):
361+
class MCPTool[TContext](FunctionTool):
364362
"""A function tool that calls an MCP service."""
365363

366364
def __init__(

astrbot/core/agent/runners/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from ..hooks import BaseAgentRunHooks
99
from ..response import AgentResponse
10-
from ..run_context import ContextWrapper, TContext
10+
from ..run_context import ContextWrapper
1111

1212

1313
class AgentState(Enum):
@@ -19,7 +19,7 @@ class AgentState(Enum):
1919
ERROR = auto() # Error state
2020

2121

22-
class BaseAgentRunner(T.Generic[TContext]):
22+
class BaseAgentRunner[TContext]:
2323
@abc.abstractmethod
2424
async def reset(
2525
self,

astrbot/core/agent/runners/coze/coze_agent_runner.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import base64
22
import json
3-
import sys
43
import typing as T
4+
from typing import override
55

66
import astrbot.core.message.components as Comp
77
from astrbot import logger
@@ -18,11 +18,6 @@
1818
from ..base import AgentResponse, AgentState, BaseAgentRunner
1919
from .coze_api_client import CozeAPIClient
2020

21-
if sys.version_info >= (3, 12):
22-
from typing import override
23-
else:
24-
from typing_extensions import override
25-
2621

2722
class CozeAgentRunner(BaseAgentRunner[TContext]):
2823
"""Coze Agent Runner"""
@@ -251,7 +246,7 @@ async def _execute_coze_request(self):
251246
conversation_id=conversation_id,
252247
auto_save_history=self.auto_save_history,
253248
stream=True,
254-
timeout=self.timeout,
249+
timeout_seconds=self.timeout,
255250
):
256251
event_type = chunk.get("event")
257252
data = chunk.get("data", {})

astrbot/core/agent/runners/coze/coze_api_client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ async def upload_file(
9090
logger.debug(f"[Coze] 图片上传成功,file_id: {file_id}")
9191
return file_id
9292

93-
except asyncio.TimeoutError:
93+
except TimeoutError:
9494
logger.error("文件上传超时")
9595
raise Exception("文件上传超时")
9696
except Exception as e:
@@ -128,7 +128,7 @@ async def chat_messages(
128128
conversation_id: str | None = None,
129129
auto_save_history: bool = True,
130130
stream: bool = True,
131-
timeout: float = 120,
131+
timeout_seconds: float = 120,
132132
) -> AsyncGenerator[dict[str, Any], None]:
133133
"""发送聊天消息并返回流式响应
134134
@@ -139,7 +139,7 @@ async def chat_messages(
139139
conversation_id: 会话ID
140140
auto_save_history: 是否自动保存历史
141141
stream: 是否流式响应
142-
timeout: 超时时间
142+
timeout_seconds: 超时时间
143143
144144
"""
145145
session = await self._ensure_session()
@@ -166,7 +166,7 @@ async def chat_messages(
166166
url,
167167
json=payload,
168168
params=params,
169-
timeout=aiohttp.ClientTimeout(total=timeout),
169+
timeout=aiohttp.ClientTimeout(total=timeout_seconds),
170170
) as response:
171171
if response.status == 401:
172172
raise Exception("Coze API 认证失败,请检查 API Key 是否正确")
@@ -203,8 +203,8 @@ async def chat_messages(
203203
except json.JSONDecodeError:
204204
event_data = {"content": data_str}
205205

206-
except asyncio.TimeoutError:
207-
raise Exception(f"Coze API 流式请求超时 ({timeout}秒)")
206+
except TimeoutError:
207+
raise Exception(f"Coze API 流式请求超时 ({timeout_seconds}秒)")
208208
except Exception as e:
209209
raise Exception(f"Coze API 流式请求失败: {e!s}")
210210

@@ -236,7 +236,7 @@ async def clear_context(self, conversation_id: str):
236236
except json.JSONDecodeError:
237237
raise Exception("Coze API 返回非JSON格式")
238238

239-
except asyncio.TimeoutError:
239+
except TimeoutError:
240240
raise Exception("Coze API 请求超时")
241241
except aiohttp.ClientError as e:
242242
raise Exception(f"Coze API 请求失败: {e!s}")

astrbot/core/agent/runners/dashscope/dashscope_agent_runner.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import functools
33
import queue
44
import re
5-
import sys
65
import threading
76
import typing as T
7+
from typing import override
88

99
from dashscope import Application
1010
from dashscope.app.application_response import ApplicationResponse
@@ -22,11 +22,6 @@
2222
from ...run_context import ContextWrapper, TContext
2323
from ..base import AgentResponse, AgentState, BaseAgentRunner
2424

25-
if sys.version_info >= (3, 12):
26-
from typing import override
27-
else:
28-
from typing_extensions import override
29-
3025

3126
class DashscopeAgentRunner(BaseAgentRunner[TContext]):
3227
"""Dashscope Agent Runner"""

astrbot/core/agent/runners/deerflow/deerflow_agent_runner.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import asyncio
22
import hashlib
33
import json
4-
import sys
54
import typing as T
65
from collections import deque
76
from dataclasses import dataclass, field
7+
from typing import override
88
from uuid import uuid4
99

1010
import astrbot.core.message.components as Comp
@@ -40,11 +40,6 @@
4040
get_message_id,
4141
)
4242

43-
if sys.version_info >= (3, 12):
44-
from typing import override
45-
else:
46-
from typing_extensions import override
47-
4843

4944
class DeerFlowAgentRunner(BaseAgentRunner[TContext]):
5045
"""DeerFlow Agent Runner via LangGraph HTTP API."""
@@ -378,7 +373,9 @@ async def _ensure_thread_id(self, session_id: str) -> str:
378373
if thread_id:
379374
return thread_id
380375

381-
thread = await self.api_client.create_thread(timeout=min(30, self.timeout))
376+
thread = await self.api_client.create_thread(
377+
timeout_seconds=min(30, self.timeout)
378+
)
382379
thread_id = thread.get("thread_id", "")
383380
if not thread_id:
384381
raise Exception(
@@ -639,7 +636,7 @@ async def _execute_deerflow_request(self):
639636
async for event in self.api_client.stream_run(
640637
thread_id=thread_id,
641638
payload=payload,
642-
timeout=self.timeout,
639+
timeout_seconds=self.timeout,
643640
):
644641
event_type = event.get("event")
645642
data = event.get("data")
@@ -666,7 +663,7 @@ async def _execute_deerflow_request(self):
666663

667664
if event_type == "end":
668665
break
669-
except (asyncio.TimeoutError, TimeoutError):
666+
except TimeoutError:
670667
logger.warning(
671668
"DeerFlow stream timed out after %ss for thread_id=%s; returning partial result.",
672669
self.timeout,

0 commit comments

Comments
 (0)