Skip to content

Commit 94bf3b8

Browse files
Fix incorrect type annotations and errors (#3250)
* Initial plan * Fix type annotation errors in cmd_conf, cmd_init, and version_comparator Co-authored-by: LIghtJUNction <106986785+LIghtJUNction@users.noreply.github.com> * Changes before error encountered Co-authored-by: LIghtJUNction <106986785+LIghtJUNction@users.noreply.github.com> * Fix more type annotation errors: change `= None` to `| None = None` Co-authored-by: LIghtJUNction <106986785+LIghtJUNction@users.noreply.github.com> * Fix final batch of type annotation errors Co-authored-by: LIghtJUNction <106986785+LIghtJUNction@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: LIghtJUNction <106986785+LIghtJUNction@users.noreply.github.com> Co-authored-by: LIghtJUNction <lightjunction.me@gmail.com>
1 parent e190bbe commit 94bf3b8

24 files changed

+92
-84
lines changed

astrbot/cli/commands/cmd_conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def set_config(key: str, value: str):
178178

179179
@conf.command(name="get")
180180
@click.argument("key", required=False)
181-
def get_config(key: str = None):
181+
def get_config(key: str | None = None):
182182
"""获取配置项的值,不提供key则显示所有可配置项"""
183183
config = _load_config()
184184

astrbot/cli/commands/cmd_init.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import asyncio
2+
from pathlib import Path
23

34
import click
45
from filelock import FileLock, Timeout
56

67
from ..utils import check_dashboard, get_astrbot_root
78

89

9-
async def initialize_astrbot(astrbot_root) -> None:
10+
async def initialize_astrbot(astrbot_root: Path) -> None:
1011
"""执行 AstrBot 初始化逻辑"""
1112
dot_astrbot = astrbot_root / ".astrbot"
1213

astrbot/cli/utils/plugin.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ def manage_plugin(
221221
raise click.ClickException(f"插件 {plugin_name} 未安装,无法更新")
222222

223223
# 备份现有插件
224-
if is_update and backup_path.exists():
224+
if is_update and backup_path is not None and backup_path.exists():
225225
shutil.rmtree(backup_path)
226-
if is_update:
226+
if is_update and backup_path is not None:
227227
shutil.copytree(target_path, backup_path)
228228

229229
try:
@@ -233,13 +233,13 @@ def manage_plugin(
233233
get_git_repo(repo_url, target_path, proxy)
234234

235235
# 更新成功,删除备份
236-
if is_update and backup_path.exists():
236+
if is_update and backup_path is not None and backup_path.exists():
237237
shutil.rmtree(backup_path)
238238
click.echo(f"插件 {plugin_name} {'更新' if is_update else '安装'}成功")
239239
except Exception as e:
240240
if target_path.exists():
241241
shutil.rmtree(target_path, ignore_errors=True)
242-
if is_update and backup_path.exists():
242+
if is_update and backup_path is not None and backup_path.exists():
243243
shutil.move(backup_path, target_path)
244244
raise click.ClickException(
245245
f"{'更新' if is_update else '安装'}插件 {plugin_name} 时出错: {e}",

astrbot/cli/utils/version_comparator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ def split_version(version):
6262
return -1
6363
if isinstance(p1, str) and isinstance(p2, int):
6464
return 1
65-
if (isinstance(p1, int) and isinstance(p2, int)) or (
66-
isinstance(p1, str) and isinstance(p2, str)
67-
):
65+
if isinstance(p1, int) and isinstance(p2, int):
66+
if p1 > p2:
67+
return 1
68+
if p1 < p2:
69+
return -1
70+
elif isinstance(p1, str) and isinstance(p2, str):
6871
if p1 > p2:
6972
return 1
7073
if p1 < p2:

astrbot/core/config/astrbot_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(
2828
self,
2929
config_path: str = ASTRBOT_CONFIG_PATH,
3030
default_config: dict = DEFAULT_CONFIG,
31-
schema: dict = None,
31+
schema: dict | None = None,
3232
):
3333
super().__init__()
3434

@@ -142,7 +142,7 @@ def check_config_integrity(self, refer_conf: dict, conf: dict, path=""):
142142

143143
return has_new
144144

145-
def save_config(self, replace_config: dict = None):
145+
def save_config(self, replace_config: dict | None = None):
146146
"""将配置写入文件
147147
148148
如果传入 replace_config,则将配置替换为 replace_config

astrbot/core/db/migration/sqlite_v3.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,11 @@ def get_filtered_conversations(
384384
self,
385385
page: int = 1,
386386
page_size: int = 20,
387-
platforms: list[str] = None,
388-
message_types: list[str] = None,
389-
search_query: str = None,
390-
exclude_ids: list[str] = None,
391-
exclude_platforms: list[str] = None,
387+
platforms: list[str] | None = None,
388+
message_types: list[str] | None = None,
389+
search_query: str | None = None,
390+
exclude_ids: list[str] | None = None,
391+
exclude_platforms: list[str] | None = None,
392392
) -> tuple[list[dict[str, Any]], int]:
393393
"""获取筛选后的对话列表"""
394394
try:

astrbot/core/persona_mgr.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ async def delete_persona(self, persona_id: str):
6868
async def update_persona(
6969
self,
7070
persona_id: str,
71-
system_prompt: str = None,
72-
begin_dialogs: list[str] = None,
73-
tools: list[str] = None,
71+
system_prompt: str | None = None,
72+
begin_dialogs: list[str] | None = None,
73+
tools: list[str] | None = None,
7474
):
7575
"""更新指定 persona 的信息。tools 参数为 None 时表示使用所有工具,空列表表示不使用任何工具"""
7676
existing_persona = await self.db.get_persona_by_id(persona_id)

astrbot/core/platform/astr_message_event.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def request_llm(
324324
image_urls: list[str] | None = None,
325325
contexts: list | None = None,
326326
system_prompt: str = "",
327-
conversation: Conversation = None,
327+
conversation: Conversation | None = None,
328328
) -> ProviderRequest:
329329
"""创建一个 LLM 请求。
330330
@@ -394,7 +394,7 @@ async def react(self, emoji: str):
394394
"""
395395
await self.send(MessageChain([Plain(emoji)]))
396396

397-
async def get_group(self, group_id: str = None, **kwargs) -> Group | None:
397+
async def get_group(self, group_id: str | None = None, **kwargs) -> Group | None:
398398
"""获取一个群聊的数据, 如果不填写 group_id: 如果是私聊消息,返回 None。如果是群聊消息,返回当前群聊的数据。
399399
400400
适配情况:

astrbot/core/platform/astrbot_message.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@dataclass
1010
class MessageMember:
1111
user_id: str # 发送者id
12-
nickname: str = None
12+
nickname: str | None = None
1313

1414
def __str__(self):
1515
# 使用 f-string 来构建返回的字符串表示形式
@@ -23,15 +23,15 @@ def __str__(self):
2323
class Group:
2424
group_id: str
2525
"""群号"""
26-
group_name: str = None
26+
group_name: str | None = None
2727
"""群名称"""
28-
group_avatar: str = None
28+
group_avatar: str | None = None
2929
"""群头像"""
30-
group_owner: str = None
30+
group_owner: str | None = None
3131
"""群主 id"""
32-
group_admins: list[str] = None
32+
group_admins: list[str] | None = None
3333
"""群管理员 id"""
34-
members: list[MessageMember] = None
34+
members: list[MessageMember] | None = None
3535
"""所有群成员"""
3636

3737
def __str__(self):

astrbot/core/platform/message_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MessageSession:
1313
"""平台适配器实例的唯一标识符。自 AstrBot v4.0.0 起,该字段实际为 platform_id。"""
1414
message_type: MessageType
1515
session_id: str
16-
platform_id: str = None
16+
platform_id: str | None = None
1717

1818
def __str__(self):
1919
return f"{self.platform_id}:{self.message_type.value}:{self.session_id}"

0 commit comments

Comments
 (0)