ActionFunc 签名不一致:concurrent_tasks 的 args 传参与标准签名冲突 #39

Open
opened 2026-06-13 19:37:08 +08:00 by orion · 0 comments
Owner

问题描述

ActionFunc 类型签名定义为 Callable[[dict], Any]compiler.py:16),即 action 函数只接受一个 context 参数。

_periodic_runnercompiler.py:155-158)中调用 action 时传入了额外参数:

# compiler.py 第 155-158 行
args = _dynamic_args(task_spec.get("args", []), context)
await action(context, *args)

这意味着如果 DSL 中 concurrent_tasksargs 非空,action 函数会被调用为 action(context, arg1, arg2, ...),与 ActionFunc 声明的 (dict) -> Any 签名不一致。

影响

  • 类型层面:类型检查器(mypy / pyright)会报告签名不匹配
  • 运行时:如果一个 action 函数同时被用作 on_enter(无参调用)和 concurrent_tasks(带参调用),函数内部无法感知额外的 args,传参静默丢失
  • 可维护性:调用者需要知道"这是个 concurrent task"才能正确编写 action 函数

现状

目前项目中没有实际使用 concurrent_tasks.args 的案例,所以是理论问题而非运行时 bug——但这是一个潜在陷阱,新开发者踩中概率高。

建议方案

方案 A(推荐):将 args 通过 context 传递,保持签名统一

# 在 _periodic_runner 中
context["_task_args"] = _dynamic_args(task_spec.get("args", []), context)
await action(context)

action 函数按需从 context["_task_args"] 中读取参数。

方案 B:明确区分两种签名

ActionFunc = Callable[[dict], Any]                    # 标准(on_enter/on_exit/transition)
ConcurrentActionFunc = Callable[..., Any]             # 并发任务(可能带 args)

方案 C:只使用 task_spec.args 做 context 注入(如 context.setdefault("_task_args", []).extend(resolved_args)),不传 *args

## 问题描述 `ActionFunc` 类型签名定义为 `Callable[[dict], Any]`(`compiler.py:16`),即 action 函数只接受一个 `context` 参数。 但 `_periodic_runner`(`compiler.py:155-158`)中调用 action 时传入了额外参数: ```python # compiler.py 第 155-158 行 args = _dynamic_args(task_spec.get("args", []), context) await action(context, *args) ``` 这意味着如果 DSL 中 `concurrent_tasks` 的 `args` 非空,action 函数会被调用为 `action(context, arg1, arg2, ...)`,与 `ActionFunc` 声明的 `(dict) -> Any` 签名不一致。 ## 影响 - **类型层面**:类型检查器(mypy / pyright)会报告签名不匹配 - **运行时**:如果一个 action 函数同时被用作 `on_enter`(无参调用)和 `concurrent_tasks`(带参调用),函数内部无法感知额外的 args,传参静默丢失 - **可维护性**:调用者需要知道"这是个 concurrent task"才能正确编写 action 函数 ## 现状 目前项目中没有实际使用 `concurrent_tasks.args` 的案例,所以是理论问题而非运行时 bug——但这是一个潜在陷阱,新开发者踩中概率高。 ## 建议方案 **方案 A(推荐)**:将 args 通过 context 传递,保持签名统一 ```python # 在 _periodic_runner 中 context["_task_args"] = _dynamic_args(task_spec.get("args", []), context) await action(context) ``` action 函数按需从 `context["_task_args"]` 中读取参数。 **方案 B**:明确区分两种签名 ```python ActionFunc = Callable[[dict], Any] # 标准(on_enter/on_exit/transition) ConcurrentActionFunc = Callable[..., Any] # 并发任务(可能带 args) ``` **方案 C**:只使用 `task_spec.args` 做 context 注入(如 `context.setdefault("_task_args", []).extend(resolved_args)`),不传 `*args`
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
bixiu/bixiweave#39
No description provided.