@@ -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