Skip to content

refactor(core): add type hints to pipeline scheduler - #9621

Merged
Soulter merged 2 commits into
AstrBotDevs:masterfrom
PersonalViolet:refactor/scheduler-type-hints
Aug 12, 2026
Merged

refactor(core): add type hints to pipeline scheduler#9621
Soulter merged 2 commits into
AstrBotDevs:masterfrom
PersonalViolet:refactor/scheduler-type-hints

Conversation

@PersonalViolet

@PersonalViolet PersonalViolet commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

在astrbot\core\pipeline\scheduler.py中为self.stages添加类型标注,让 IDE 与类型检查器能正确推断list[Stage]类型,纯类型层面改动,无功能变化。

Modifications / 改动点

  • astrbot/core/pipeline/scheduler.py
    • self.stages 标注为 list[Stage]
    • _process_stages 中,使用 cast(AsyncGenerator[None], coroutine) 来收窄 Stage.process() 返回的生成器类型
    • 导入 Stagecast
  • This is NOT a breaking change. / 这不是一个破坏性变更。

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

uv run pytest tests/test_smoke.py -v
屏幕截图 2026-08-11 010528


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 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.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Refine the pipeline scheduler’s stage handling to use explicit typing for stage instances and their async generators.

Enhancements:

  • Annotate the scheduler’s stages collection with a concrete list[Stage] type to improve IDE support and static checking.
  • Constrain the type of async generators returned from Stage.process() using typing.cast for more precise type inference.

Copilot AI lite review requested due to automatic review settings August 10, 2026 17:09
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Aug 10, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

  • The cast(AsyncGenerator[None], coroutine) line is currently using AsyncGenerator from collections.abc, which is not subscriptable; consider importing AsyncGenerator from typing (with proper type parameters) or adjusting the cast to a compatible generic type so type checkers don't error on this annotation.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `cast(AsyncGenerator[None], coroutine)` line is currently using `AsyncGenerator` from `collections.abc`, which is not subscriptable; consider importing `AsyncGenerator` from `typing` (with proper type parameters) or adjusting the cast to a compatible generic type so type checkers don't error on this annotation.

## Individual Comments

### Comment 1
<location path="astrbot/core/pipeline/scheduler.py" line_range="53-54" />
<code_context>
             if isinstance(coroutine, AsyncGenerator):
                 # 如果返回的是异步生成器, 实现洋葱模型的核心
-                async for _ in coroutine:
+                agen = cast(AsyncGenerator[None], coroutine)
+                async for _ in agen:
                     # 此处是前置处理完成后的暂停点(yield), 下面开始执行后续阶段
                     if event.is_stopped():
</code_context>
<issue_to_address>
**issue (bug_risk):** Fix the AsyncGenerator type parameters in cast to avoid runtime/type-checking issues.

`collections.abc.AsyncGenerator` requires two type parameters `(YieldType, SendType)`, so `AsyncGenerator[None]` is invalid and can raise `TypeError` at runtime as well as fail static checks. Use something like `cast(AsyncGenerator[Any, Any], coroutine)` (or `AsyncGenerator[object, object]` if that’s more accurate), or drop the cast and rely on the `isinstance(..., AsyncGenerator)` check if your type checker supports that narrowing.
</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.

Comment thread astrbot/core/pipeline/scheduler.py

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to improve static typing for the pipeline scheduler by explicitly annotating PipelineScheduler.stages as list[Stage] and narrowing the inferred type of the async generator returned by Stage.process().

Changes:

  • Add Stage typing for self.stages to improve IDE/type-checker inference.
  • Introduce a typing.cast in _process_stages to narrow the async generator type returned from Stage.process().
  • Update imports to include cast and Stage.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread astrbot/core/pipeline/scheduler.py
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Aug 10, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Aug 12, 2026
@Soulter
Soulter merged commit dbc0e71 into AstrBotDevs:master Aug 12, 2026
21 checks passed
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 lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants