Merged
fix(front): fix issue #3272 and #3274 add assemble parameter validation #6170
fix(front): fix issue #3272 and #3274 add assemble parameter validation #6170
Merged
HuYiShengAdd on Sep 11
HuYiSheng
HuYiShengCollaborators
Sep 11

主仓合入后,同步合入到商仓:
额外修复了一个之前提交的文档的问题#3245
本 PR 包含 issue #3272 与 #3274 的两项配套修复:pypto.assemble 前端实现(operation.py)对参数数量与类型零校验,用户按 @overload 签名调用时,任何偏差都会泄露 Python 内部解包/迭代错误(无函数名、无签名提示、无源码位置)。

  • #3272:参数顺序/类型传错(如 offsets 位置传 Tensor)时,深层调用链报 FeError: Tensor is not iterable 等内部错误,与用户实际意图完全脱节;

  • #3274:模块级 assemble 声明了 @overload 签名但运行时零校验,且缺少 @source_location 装饰器(对比 Tensor.assemble 方法),错误无法定位到用户调用点。

  • assemble() 入口显式校验参数数量(单源 3 参 / 多源 2 参),其余情况抛出语义化错误;

  • 校验 src/dst 为 pypto.Tensor、offsets 为 list/tuple、srcs 为序列,错误信息包含函数名、参数位置、期望类型与实际类型;

  • 2 参形式且首参为 Tensor 时,附加提示"可能想使用单源形式但缺少 offsets 参数"。

Related Issue: #3272, #3274 #3245

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for HuYiSheng's contribution.)
HuYiShengHuYiShengCollaborators
Sep 11 Created pull request, commit 21e139d9
HuYiShengHuYiShengCollaborators
Sep 11 Associated issue: [Bug-Report|缺陷反馈]: 嵌套循环中 view[:]= 写回静默不执行,但无任何报错信息提示,文档中无相关描述,直到数据校验才校验失败。,[Bug-Report|缺陷反馈]:【易用性】assemble() 无参数校验,API 误用时产生内部崩溃式错误,报错信息不清晰,请修改。,[Bug-Report|缺陷反馈]:【易用性】pypto.assemble 的 @overload 签名仅用于文档,运行时零校验,API 误用时报内部解包错误,且报错信息模糊无法指导定位
atomgit-bot
atomgit-bot
Sep 11 Comments:

变更摘要

本 PR 针对 issue #3272 与 #3274(并附带修复文档问题 #3245)为 pypto.assemble 前端实现补充参数校验。此前 assemble() 对参数数量与类型零校验,用户按 @overload 签名之外的任何方式调用时,会泄露诸如 FeError: Tensor is not iterable 之类的内部解包/迭代错误,既无函数名、签名提示,也无源码位置。本次改动在 python/pypto/operation.py 的 assemble() 入口显式校验参数数量(单源 3 参 src, offsets, dst / 多源 2 参 srcs, dst),并通过新增的 check_type 工具校验 src/dst 为 pypto.Tensor、offsets 为 Sequence[int | SymbolicScalar]、srcs 项为 (Tensor, offsets) 二元组,错误信息包含函数名、参数位置、期望类型与实际类型;同时为 assemble() 加上 @source_location 装饰器,使错误可定位到用户调用点。对 2 参形式且首参为 Tensor 的情况,额外提示"可能想使用单源形式但缺少 offsets 参数"。

主要改动

  • 新增 check_type 类型校验工具: 在 python/pypto/_utils.py 中新增 check_type(value, expect_type, arg_name, expect_len),支持对 typing 泛型来源类型(list/Sequence/tuple/dict)、元素类型、types.UnionType 联合类型及长度进行校验,不匹配时抛出包含参数名、期望类型与实际类型的 TypeError。

  • assemble() 参数数量显式校验: 在 python/pypto/operation.py 中,assemble() 仅接受 3 参(单源形式)或 2 参(多源形式),其余数量(如 0 参、4 参)抛出语义化 TypeError("expects 3 positional arguments … or 2 positional arguments …, but got N"),不再落入内部解包逻辑。

  • 类型校验与提示信息增强: 校验 src/dst 为 Tensor、offsets 为 Sequence[int | SymbolicScalar]、多源 srcs 每项为 (Tensor, offsets) 二元组;当 2 参形式首参为 Tensor 时附加"missing the 'offsets' argument"提示;并为 assemble() 增加 @source_location 装饰器以便定位用户调用点。

  • 新增配套单元测试: 在 python/tests/ut/interface/test_dynamic_shape_error.py 中新增测试,覆盖参数数量与缺失 offsets 的错误提示(test_assemble_arg_count_and_hint)、合法单源/多源调用不被误拦截(test_assemble_valid_calls_not_blocked),以及 check_type 的通过(test_check_type_pass)与失败(test_check_type_fail)两类用例。

likedislike
Incorrect?
atomgit-bot
atomgit-bot
Sep 11 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 11 Added label: stat/needs-squash
41 messages are collapsed here。 See more
lyz0328
lyz0328Collaborators
Sep 11 Comments:

/approve

likedislike
杨旭
杨旭Collaborators
Sep 11 Comments:

/lgtm
/approve

likedislike
lidongsheng
lidongshengCollaborators
Sep 11 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Sep 11 Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
Sep 11 Merged pull request