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

@@ -3,7 +3,7 @@ import os
import shutil
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any
from typing import Any, ClassVar
import toml
@@ -30,11 +30,11 @@ class PluginBase(ABC):
config_file_name: str
enable_plugin: bool = True
config_schema: dict[str, dict[str, ConfigField] | str] = {}
config_schema: ClassVar[dict[str, dict[str, ConfigField] | str] ] = {}
permission_nodes: list["PermissionNodeField"] = []
permission_nodes: ClassVar[list["PermissionNodeField"] ] = []
config_section_descriptions: dict[str, str] = {}
config_section_descriptions: ClassVar[dict[str, str] ] = {}
def __init__(self, plugin_dir: str, metadata: PluginMetadata):
"""初始化插件
@@ -206,12 +206,12 @@ class PluginBase(ABC):
if not self.config_schema:
return {}
config_data = {}
config_data: ClassVar = {}
# 遍历每个配置节
for section, fields in self.config_schema.items():
if isinstance(fields, dict):
section_data = {}
section_data: ClassVar = {}
# 遍历节内的字段
for field_name, field in fields.items():
@@ -331,7 +331,7 @@ class PluginBase(ABC):
try:
with open(user_config_path, encoding="utf-8") as f:
user_config = toml.load(f) or {}
user_config: ClassVar = toml.load(f) or {}
except Exception as e:
logger.error(f"{self.log_prefix} 加载用户配置文件 {user_config_path} 失败: {e}", exc_info=True)
self.config = self._generate_config_from_schema() # 加载失败时使用默认 schema