完成所有类型注解的修复
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import shutil
|
||||
import tomlkit
|
||||
from tomlkit.items import Table
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
|
||||
@@ -45,8 +46,8 @@ def update_config():
|
||||
|
||||
# 检查version是否相同
|
||||
if old_config and "inner" in old_config and "inner" in new_config:
|
||||
old_version = old_config["inner"].get("version")
|
||||
new_version = new_config["inner"].get("version")
|
||||
old_version = old_config["inner"].get("version") # type: ignore
|
||||
new_version = new_config["inner"].get("version") # type: ignore
|
||||
if old_version and new_version and old_version == new_version:
|
||||
print(f"检测到版本号相同 (v{old_version}),跳过更新")
|
||||
# 如果version相同,恢复旧配置文件并返回
|
||||
@@ -62,7 +63,7 @@ def update_config():
|
||||
if key == "version":
|
||||
continue
|
||||
if key in target:
|
||||
if isinstance(value, dict) and isinstance(target[key], (dict, tomlkit.items.Table)):
|
||||
if isinstance(value, dict) and isinstance(target[key], (dict, Table)):
|
||||
update_dict(target[key], value)
|
||||
else:
|
||||
try:
|
||||
@@ -85,10 +86,7 @@ def update_config():
|
||||
if value and isinstance(value[0], dict) and "regex" in value[0]:
|
||||
contains_regex = True
|
||||
|
||||
if contains_regex:
|
||||
target[key] = value
|
||||
else:
|
||||
target[key] = tomlkit.array(value)
|
||||
target[key] = value if contains_regex else tomlkit.array(str(value))
|
||||
else:
|
||||
# 其他类型使用item方法创建新值
|
||||
target[key] = tomlkit.item(value)
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
import os
|
||||
from dataclasses import field, dataclass
|
||||
|
||||
import tomlkit
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
|
||||
from datetime import datetime
|
||||
from tomlkit import TOMLDocument
|
||||
from tomlkit.items import Table
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from dataclasses import field, dataclass
|
||||
from rich.traceback import install
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config_base import ConfigBase
|
||||
from src.config.official_configs import (
|
||||
BotConfig,
|
||||
@@ -80,8 +78,8 @@ def update_config():
|
||||
|
||||
# 检查version是否相同
|
||||
if old_config and "inner" in old_config and "inner" in new_config:
|
||||
old_version = old_config["inner"].get("version")
|
||||
new_version = new_config["inner"].get("version")
|
||||
old_version = old_config["inner"].get("version") # type: ignore
|
||||
new_version = new_config["inner"].get("version") # type: ignore
|
||||
if old_version and new_version and old_version == new_version:
|
||||
logger.info(f"检测到配置文件版本号相同 (v{old_version}),跳过更新")
|
||||
return
|
||||
@@ -103,7 +101,7 @@ def update_config():
|
||||
shutil.copy2(template_path, new_config_path)
|
||||
logger.info(f"已创建新配置文件: {new_config_path}")
|
||||
|
||||
def update_dict(target: TOMLDocument | dict, source: TOMLDocument | dict):
|
||||
def update_dict(target: TOMLDocument | dict | Table, source: TOMLDocument | dict):
|
||||
"""
|
||||
将source字典的值更新到target字典中(如果target中存在相同的键)
|
||||
"""
|
||||
@@ -112,8 +110,9 @@ def update_config():
|
||||
if key == "version":
|
||||
continue
|
||||
if key in target:
|
||||
if isinstance(value, dict) and isinstance(target[key], (dict, Table)):
|
||||
update_dict(target[key], value)
|
||||
target_value = target[key]
|
||||
if isinstance(value, dict) and isinstance(target_value, (dict, Table)):
|
||||
update_dict(target_value, value)
|
||||
else:
|
||||
try:
|
||||
# 对数组类型进行特殊处理
|
||||
|
||||
@@ -43,7 +43,7 @@ class ConfigBase:
|
||||
field_type = f.type
|
||||
|
||||
try:
|
||||
init_args[field_name] = cls._convert_field(value, field_type)
|
||||
init_args[field_name] = cls._convert_field(value, field_type) # type: ignore
|
||||
except TypeError as e:
|
||||
raise TypeError(f"Field '{field_name}' has a type error: {e}") from e
|
||||
except Exception as e:
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Literal
|
||||
import re
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Literal, Optional
|
||||
|
||||
from src.config.config_base import ConfigBase
|
||||
|
||||
"""
|
||||
@@ -113,7 +114,7 @@ class ChatConfig(ConfigBase):
|
||||
exit_focus_threshold: float = 1.0
|
||||
"""自动退出专注聊天的阈值,越低越容易退出专注聊天"""
|
||||
|
||||
def get_current_talk_frequency(self, chat_stream_id: str = None) -> float:
|
||||
def get_current_talk_frequency(self, chat_stream_id: Optional[str] = None) -> float:
|
||||
"""
|
||||
根据当前时间和聊天流获取对应的 talk_frequency
|
||||
|
||||
@@ -138,7 +139,7 @@ class ChatConfig(ConfigBase):
|
||||
# 如果都没有匹配,返回默认值
|
||||
return self.talk_frequency
|
||||
|
||||
def _get_time_based_frequency(self, time_freq_list: list[str]) -> float:
|
||||
def _get_time_based_frequency(self, time_freq_list: list[str]) -> Optional[float]:
|
||||
"""
|
||||
根据时间配置列表获取当前时段的频率
|
||||
|
||||
@@ -186,7 +187,7 @@ class ChatConfig(ConfigBase):
|
||||
|
||||
return current_frequency
|
||||
|
||||
def _get_stream_specific_frequency(self, chat_stream_id: str) -> float:
|
||||
def _get_stream_specific_frequency(self, chat_stream_id: str):
|
||||
"""
|
||||
获取特定聊天流在当前时间的频率
|
||||
|
||||
@@ -217,7 +218,7 @@ class ChatConfig(ConfigBase):
|
||||
|
||||
return None
|
||||
|
||||
def _parse_stream_config_to_chat_id(self, stream_config_str: str) -> str:
|
||||
def _parse_stream_config_to_chat_id(self, stream_config_str: str) -> Optional[str]:
|
||||
"""
|
||||
解析流配置字符串并生成对应的 chat_id
|
||||
|
||||
|
||||
Reference in New Issue
Block a user