Skip to content

fix: pyright lint#4874

Merged
Soulter merged 7 commits intoAstrBotDevs:masterfrom
Dt8333:fix-pyright
Feb 5, 2026
Merged

fix: pyright lint#4874
Soulter merged 7 commits intoAstrBotDevs:masterfrom
Dt8333:fix-pyright

Conversation

@Dt8333
Copy link
Member

@Dt8333 Dt8333 commented Feb 5, 2026

RT,修复pyright报错。

Modifications / 改动点

用field标注platform_id,避免声明None.
添加兜底,适配py-cord>=2.7.0同时保证前向兼容.
修改部分default值和声明

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

0 errors, 0 warnings, 0 informations


Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

Summary by Sourcery

修复了 Discord 适配器、仪表盘路由、内容安全策略、消息会话以及 provider 源模块中的 Pyright 类型检查问题。

Bug 修复:

  • 通过在频道不可用时添加回退逻辑,防止在 Discord 动态回调中访问为 None 的 channel。
  • 对 Baidu AIP 内容安全的返回结果显式转换为 dict,以满足类型检查要求并避免错误的序列假设。
  • 在 FishAudio TTS provider 配置中避免将 None 作为 model 值传入,默认改为使用空字符串。
  • 修正知识库路由中的任务结果类型,将无效的 any 注解改为使用 Any

增强:

  • 将消息会话的 platform_id 设为非可选字段,并通过 dataclass 的 field metadata 进行初始化。
  • 收紧仪表盘定时任务(cron)路由序列化返回值的类型标注,并为 psutil 本地地址引入带类型的 Protocol,以满足 Pyright 在端口检查逻辑中的要求。
Original summary in English

Summary by Sourcery

Fix Pyright type-checking issues across Discord adapter, dashboard routes, content safety strategy, message session, and provider source modules.

Bug Fixes:

  • Prevent None-channel access in Discord dynamic callbacks by adding fallbacks when channel is unavailable.
  • Ensure Baidu AIP content safety responses are handled with explicit dict casting to satisfy type checking and avoid incorrect sequence assumptions.
  • Avoid passing None as a model value in FishAudio TTS provider configuration by defaulting to an empty string.
  • Correct task result typing in knowledge base routes to use Any instead of the invalid any annotation.

Enhancements:

  • Make message session platform_id a non-optional field initialized via dataclass field metadata.
  • Tighten dashboard cron route serialization return typing and introduce a typed protocol for psutil local addresses to appease Pyright in port-inspection logic.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Feb 5, 2026
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了 1 个问题,并留下了一些整体性的反馈:

  • MessageSession 中,platform_id 现在被设为 field(init=False),但仍在 __str__ 中使用;建议添加一个 __post_init__ 或专门的工厂/初始化函数,以确保在任何字符串化之前都已设置该字段,从而避免运行时出现 AttributeError
  • FishAudioTTSAPISource 中,将默认 modelNone 修改为空字符串会改变 set_model 的行为;请再次确认 "" 是否与之前的 None 情况等价处理,并且不会触发意外的模型选择或错误。
  • dynamic_callback 的回退路径中,当 ctx.channelNone 时,你可能需要显式记录日志或追踪该分支,因为这很可能是异常场景,有助于诊断某些缺失频道信息的异常 Discord 上下文。
给 AI Agent 的提示词
Please address the comments from this code review:

## Overall Comments
- In `MessageSession`, `platform_id` is now `field(init=False)` but still used in `__str__`; consider adding a `__post_init__` or dedicated factory/initializer to guarantee it is set before any stringification to avoid runtime `AttributeError`.
- The change in `FishAudioTTSAPISource` from `None` to an empty string for the default `model` may alter `set_model` behavior; double-check that `""` is handled equivalently to the previous `None` case and won’t trigger unexpected model selection or errors.
- In the `dynamic_callback` fallback path where `ctx.channel` is `None`, you might want to log or trace this branch explicitly since it is likely exceptional and could help diagnose odd Discord contexts where channel details are missing.

## Individual Comments

### Comment 1
<location> `astrbot/core/platform/message_session.py:16` </location>
<code_context>
     message_type: MessageType
     session_id: str
-    platform_id: str | None = None
+    platform_id: str = field(init=False)

     def __str__(self):
</code_context>

<issue_to_address>
**issue (bug_risk):** Using `field(init=False)` for `platform_id` makes the attribute potentially missing at runtime when `__str__` is called.

Because `platform_id` is `init=False`, it won’t exist on instances created via the dataclass `__init__`, yet `__str__` always accesses `self.platform_id`. Any code that prints/logs a `MessageSession` before manually setting `platform_id` will now get an `AttributeError` instead of a `None` value. Please either keep a default (e.g. `platform_id: str | None = None`) or initialize it in `__post_init__`/a custom constructor so it’s always set before use.
</issue_to_address>

Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请对每条评论点选 👍 或 👎,我会根据你的反馈改进后续评审。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • In MessageSession, platform_id is now field(init=False) but still used in __str__; consider adding a __post_init__ or dedicated factory/initializer to guarantee it is set before any stringification to avoid runtime AttributeError.
  • The change in FishAudioTTSAPISource from None to an empty string for the default model may alter set_model behavior; double-check that "" is handled equivalently to the previous None case and won’t trigger unexpected model selection or errors.
  • In the dynamic_callback fallback path where ctx.channel is None, you might want to log or trace this branch explicitly since it is likely exceptional and could help diagnose odd Discord contexts where channel details are missing.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `MessageSession`, `platform_id` is now `field(init=False)` but still used in `__str__`; consider adding a `__post_init__` or dedicated factory/initializer to guarantee it is set before any stringification to avoid runtime `AttributeError`.
- The change in `FishAudioTTSAPISource` from `None` to an empty string for the default `model` may alter `set_model` behavior; double-check that `""` is handled equivalently to the previous `None` case and won’t trigger unexpected model selection or errors.
- In the `dynamic_callback` fallback path where `ctx.channel` is `None`, you might want to log or trace this branch explicitly since it is likely exceptional and could help diagnose odd Discord contexts where channel details are missing.

## Individual Comments

### Comment 1
<location> `astrbot/core/platform/message_session.py:16` </location>
<code_context>
     message_type: MessageType
     session_id: str
-    platform_id: str | None = None
+    platform_id: str = field(init=False)

     def __str__(self):
</code_context>

<issue_to_address>
**issue (bug_risk):** Using `field(init=False)` for `platform_id` makes the attribute potentially missing at runtime when `__str__` is called.

Because `platform_id` is `init=False`, it won’t exist on instances created via the dataclass `__init__`, yet `__str__` always accesses `self.platform_id`. Any code that prints/logs a `MessageSession` before manually setting `platform_id` will now get an `AttributeError` instead of a `None` value. Please either keep a default (e.g. `platform_id: str | None = None`) or initialize it in `__post_init__`/a custom constructor so it’s always set before use.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot dosubot bot added area:core The bug / feature is about astrbot's core, backend area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. feature:knowledge-base The bug / feature is about knowledge base labels Feb 5, 2026
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 5, 2026
@Soulter Soulter changed the title chore: 修复Pyright错误 fix: pyright lint Feb 5, 2026
@Soulter Soulter merged commit f3397f6 into AstrBotDevs:master Feb 5, 2026
6 checks passed
LIghtJUNction added a commit that referenced this pull request Feb 5, 2026
* 默认host修改为::,同时新增两个环境变量DASHBOARD_HOST,DASHBOARD_ENABLE,和DASHBOARD_PORT对齐

* feat: systemd support (#4880)

* fix: pyright lint (#4874)

* feat: 将 MessageSession 的 platform_id 改为 init=False,实例化时无需传入

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

* refactor: 将 isinstance 检查改为元组、将默认模型值设为空字符串、将类型注解改为 Any 并导入

* refactor: 为 _serialize_job 增加返回类型注解 dict

* fix: 使用 cast 获取百度 AIP 的 msg 并对 psutil_addr 引入 type: ignore

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

* refactor: 引入 _AddrWithPort 协议并替换 conn.laddr 的 cast

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

* fix: 在构建 AstrBotMessage 时对 ctx.channel 可能为 None 进行兜底处理

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

---------

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

* fix: TypeError when MCP schema type is a list (#4867)

* Fix TypeError when MCP schema type is a list

Fixes crash in Gemini native tools with VRChat MCP.

* Refactor: avoid modifying schema in place per feedback

* Fix formatting and cleanup comments

* docs: update watashiwakoseinodesukara

Removed duplicate text and added a new image.

* 修复/跨平台一致性

* 琐事/类型标注和一些简单错误修正

* 修复/检查端口时候包含ipv6

* 修复/enable变量的赋值逻辑

---------

Co-authored-by: Dt8333 <25431943+Dt8333@users.noreply.github.com>
Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>
Co-authored-by: boushi1111 <95118141+boushi1111@users.noreply.github.com>
Co-authored-by: Soulter <37870767+Soulter@users.noreply.github.com>
@Dt8333 Dt8333 deleted the fix-pyright branch February 6, 2026 08:01
united-pooh pushed a commit to united-pooh/AstrBot that referenced this pull request Feb 19, 2026
* feat: 将 MessageSession 的 platform_id 改为 init=False,实例化时无需传入

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

* refactor: 将 isinstance 检查改为元组、将默认模型值设为空字符串、将类型注解改为 Any 并导入

* refactor: 为 _serialize_job 增加返回类型注解 dict

* fix: 使用 cast 获取百度 AIP 的 msg 并对 psutil_addr 引入 type: ignore

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

* refactor: 引入 _AddrWithPort 协议并替换 conn.laddr 的 cast

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

* fix: 在构建 AstrBotMessage 时对 ctx.channel 可能为 None 进行兜底处理

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

---------

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>
miaoxutao123 pushed a commit to miaoxutao123/AstrBot that referenced this pull request Feb 28, 2026
* 默认host修改为::,同时新增两个环境变量DASHBOARD_HOST,DASHBOARD_ENABLE,和DASHBOARD_PORT对齐

* feat: systemd support (AstrBotDevs#4880)

* fix: pyright lint (AstrBotDevs#4874)

* feat: 将 MessageSession 的 platform_id 改为 init=False,实例化时无需传入

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

* refactor: 将 isinstance 检查改为元组、将默认模型值设为空字符串、将类型注解改为 Any 并导入

* refactor: 为 _serialize_job 增加返回类型注解 dict

* fix: 使用 cast 获取百度 AIP 的 msg 并对 psutil_addr 引入 type: ignore

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

* refactor: 引入 _AddrWithPort 协议并替换 conn.laddr 的 cast

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

* fix: 在构建 AstrBotMessage 时对 ctx.channel 可能为 None 进行兜底处理

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

---------

Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>

* fix: TypeError when MCP schema type is a list (AstrBotDevs#4867)

* Fix TypeError when MCP schema type is a list

Fixes crash in Gemini native tools with VRChat MCP.

* Refactor: avoid modifying schema in place per feedback

* Fix formatting and cleanup comments

* docs: update watashiwakoseinodesukara

Removed duplicate text and added a new image.

* 修复/跨平台一致性

* 琐事/类型标注和一些简单错误修正

* 修复/检查端口时候包含ipv6

* 修复/enable变量的赋值逻辑

---------

Co-authored-by: Dt8333 <25431943+Dt8333@users.noreply.github.com>
Co-authored-by: aider (openai/gpt-5.2) <aider@aider.chat>
Co-authored-by: boushi1111 <95118141+boushi1111@users.noreply.github.com>
Co-authored-by: Soulter <37870767+Soulter@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. feature:knowledge-base The bug / feature is about knowledge base lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants