Skip to content

Commit c414039

Browse files
committed
chore(core.config): add missing type annotations
1 parent 5ea6e13 commit c414039

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

astrbot/core/astrbot_config_mgr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(
3535
default_config: AstrBotConfig,
3636
ucr: UmopConfigRouter,
3737
sp: SharedPreferences,
38-
):
38+
) -> None:
3939
self.sp = sp
4040
self.ucr = ucr
4141
self.confs: dict[str, AstrBotConfig] = {}
@@ -52,7 +52,7 @@ def _get_abconf_data(self) -> dict:
5252
)
5353
return self.abconf_data
5454

55-
def _load_all_configs(self):
55+
def _load_all_configs(self) -> None:
5656
"""Load all configurations from the shared preferences."""
5757
abconf_data = self._get_abconf_data()
5858
self.abconf_data = abconf_data

astrbot/core/config/astrbot_config.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ class AstrBotConfig(dict):
2222
- 如果传入了 schema,将会通过 schema 解析出 default_config,此时传入的 default_config 会被忽略。
2323
"""
2424

25+
# Class-level 属性注解,帮助类型检查器正确推断实例属性类型
26+
config_path: str
27+
default_config: dict
28+
schema: dict | None
29+
first_deploy: bool | None
30+
2531
def __init__(
2632
self,
2733
config_path: str = ASTRBOT_CONFIG_PATH,
2834
default_config: dict = DEFAULT_CONFIG,
29-
schema: dict = None,
30-
):
35+
schema: dict | None = None,
36+
) -> None:
3137
super().__init__()
32-
3338
# 调用父类的 __setattr__ 方法,防止保存配置时将此属性写入配置文件
3439
object.__setattr__(self, "config_path", config_path)
3540
object.__setattr__(self, "default_config", default_config)
@@ -60,7 +65,7 @@ def _config_schema_to_default_config(self, schema: dict) -> dict:
6065
"""将 Schema 转换成 Config"""
6166
conf = {}
6267

63-
def _parse_schema(schema: dict, conf: dict):
68+
def _parse_schema(schema: dict, conf: dict) -> None:
6469
for k, v in schema.items():
6570
if v["type"] not in DEFAULT_VALUE_MAP:
6671
raise TypeError(
@@ -81,7 +86,9 @@ def _parse_schema(schema: dict, conf: dict):
8186

8287
return conf
8388

84-
def check_config_integrity(self, refer_conf: dict, conf: dict, path=""):
89+
def check_config_integrity(
90+
self, refer_conf: dict, conf: dict, path: str = ""
91+
) -> bool:
8592
"""检查配置完整性,如果有新的配置项或顺序不一致则返回 True"""
8693
has_new = False
8794

@@ -139,7 +146,7 @@ def check_config_integrity(self, refer_conf: dict, conf: dict, path=""):
139146

140147
return has_new
141148

142-
def save_config(self, replace_config: dict = None):
149+
def save_config(self, replace_config: dict | None = None) -> None:
143150
"""将配置写入文件
144151
145152
如果传入 replace_config,则将配置替换为 replace_config
@@ -149,20 +156,20 @@ def save_config(self, replace_config: dict = None):
149156
with open(self.config_path, "w", encoding="utf-8-sig") as f:
150157
json.dump(self, f, indent=2, ensure_ascii=False)
151158

152-
def __getattr__(self, item):
159+
def __getattr__(self, item: str) -> object:
153160
try:
154161
return self[item]
155162
except KeyError:
156163
return None
157164

158-
def __delattr__(self, key):
165+
def __delattr__(self, key: str) -> None:
159166
try:
160167
del self[key]
161168
self.save_config()
162169
except KeyError:
163170
raise AttributeError(f"没有找到 Key: '{key}'")
164171

165-
def __setattr__(self, key, value):
172+
def __setattr__(self, key: str, value: object) -> None:
166173
self[key] = value
167174

168175
def check_exist(self) -> bool:

0 commit comments

Comments
 (0)