refactor: 统一类型注解风格并优化代码结构

- 将裸 except 改为显式 Exception 捕获
- 用列表推导式替换冗余 for 循环
- 为类属性添加 ClassVar 注解
- 统一 Union/Optional 写法为 |
- 移除未使用的导入
- 修复 SQLAlchemy 空值比较语法
- 优化字符串拼接与字典更新逻辑
- 补充缺失的 noqa 注释与异常链

BREAKING CHANGE: 所有插件基类的类级字段现要求显式 ClassVar 注解,自定义插件需同步更新
This commit is contained in:
明天好像没什么
2025-10-31 22:42:39 +08:00
parent 5080cfccfc
commit 0e129d385e
105 changed files with 592 additions and 561 deletions

View File

@@ -48,9 +48,10 @@ class ChatManager:
raise TypeError("platform 必须是字符串或是 SpecialTypes 枚举")
streams = []
try:
for stream in get_chat_manager().streams.values():
if platform == SpecialTypes.ALL_PLATFORMS or stream.platform == platform:
streams.append(stream)
streams.extend(
stream for stream in get_chat_manager().streams.values()
if platform == SpecialTypes.ALL_PLATFORMS or stream.platform == platform
)
logger.debug(f"[ChatAPI] 获取到 {len(streams)}{platform} 平台的聊天流")
except Exception as e:
logger.error(f"[ChatAPI] 获取聊天流失败: {e}")
@@ -71,9 +72,10 @@ class ChatManager:
raise TypeError("platform 必须是字符串或是 SpecialTypes 枚举")
streams = []
try:
for stream in get_chat_manager().streams.values():
if (platform == SpecialTypes.ALL_PLATFORMS or stream.platform == platform) and stream.group_info:
streams.append(stream)
streams.extend(
stream for stream in get_chat_manager().streams.values()
if (platform == SpecialTypes.ALL_PLATFORMS or stream.platform == platform) and stream.group_info
)
logger.debug(f"[ChatAPI] 获取到 {len(streams)}{platform} 平台的群聊流")
except Exception as e:
logger.error(f"[ChatAPI] 获取群聊流失败: {e}")
@@ -97,9 +99,10 @@ class ChatManager:
raise TypeError("platform 必须是字符串或是 SpecialTypes 枚举")
streams = []
try:
for stream in get_chat_manager().streams.values():
if (platform == SpecialTypes.ALL_PLATFORMS or stream.platform == platform) and not stream.group_info:
streams.append(stream)
streams.extend(
stream for stream in get_chat_manager().streams.values()
if (platform == SpecialTypes.ALL_PLATFORMS or stream.platform == platform) and not stream.group_info
)
logger.debug(f"[ChatAPI] 获取到 {len(streams)}{platform} 平台的私聊流")
except Exception as e:
logger.error(f"[ChatAPI] 获取私聊流失败: {e}")

View File

@@ -183,9 +183,10 @@ async def build_cross_context_s4u(
blacklisted_streams.add(stream_id)
except ValueError:
logger.warning(f"无效的S4U黑名单格式: {chat_str}")
for stream_id in chat_manager.streams:
if stream_id != chat_stream.stream_id and stream_id not in blacklisted_streams:
streams_to_scan.append(stream_id)
streams_to_scan.extend(
stream_id for stream_id in chat_manager.streams
if stream_id != chat_stream.stream_id and stream_id not in blacklisted_streams
)
logger.debug(f"[S4U] Found {len(streams_to_scan)} group streams to scan.")

View File

@@ -47,7 +47,7 @@ class ScoringAPI:
return await relationship_service.get_user_relationship_data(user_id)
@staticmethod
async def update_user_relationship(user_id: str, relationship_score: float, relationship_text: str = None, user_name: str = None):
async def update_user_relationship(user_id: str, relationship_score: float, relationship_text: str | None = None, user_name: str | None = None):
"""
更新用户关系数据
@@ -71,7 +71,7 @@ class ScoringAPI:
await interest_service.initialize_smart_interests(personality_description, personality_id)
@staticmethod
async def calculate_interest_match(content: str, keywords: list[str] = None):
async def calculate_interest_match(content: str, keywords: list[str] | None = None):
"""
计算内容与兴趣的匹配度
@@ -98,7 +98,7 @@ class ScoringAPI:
}
@staticmethod
def clear_caches(user_id: str = None):
def clear_caches(user_id: str | None = None):
"""
清理缓存

View File

@@ -9,7 +9,7 @@
import json
import os
import threading
from typing import Any
from typing import Any, ClassVar
from src.common.logger import get_logger
@@ -26,7 +26,7 @@ class PluginStorageManager:
现在它和API住在一起了希望它们能和睦相处。
"""
_instances: dict[str, "PluginStorage"] = {}
_instances: ClassVar[dict[str, "PluginStorage"] ] = {}
_lock = threading.Lock()
_base_path = os.path.join("data", "plugin_data")

View File

@@ -9,11 +9,11 @@ logger = get_logger("tool_api")
def get_tool_instance(tool_name: str, chat_stream: Any = None) -> BaseTool | None:
"""获取公开工具实例
Args:
tool_name: 工具名称
chat_stream: 聊天流对象,用于提供上下文信息
Returns:
BaseTool: 工具实例如果工具不存在则返回None
"""