Closed
[Bug-Report|缺陷反馈]:【易用性】pypto.assemble 的 @overload 签名仅用于文档,运行时零校验,API 误用时报内部解包错误,且报错信息模糊无法指导定位 #3274
liuxiaoxiao11Add on  Sep 9Closed at  Sep 11
liuxiaoxiao11
liuxiaoxiao11Collaborators
Sep 9 Create

Thanks for sending an issue! Please fill in the following template to help quickly solve your problem.

Describe the current behavior / 问题描述 (Mandatory / 必填)

pypto.assemble 声明了两套 @overload 签名(operation.py:29-73 单源模式、:76-118 多源模式),IDE 静态检查会按此提示用户。但实际实现是 def
assemble(*args, parallel: bool = False)(operation.py:121),运行时对参数数量、类型、两种模式的分派完全零校验。用户按 @overload 签名调用时,
任何偏差都触发 Python 内部的解包/迭代错误,错误信息不含函数名、签名提示或源代码位置。

Environment / 环境信息 (Mandatory / 必填)

服务器/NPU 型号: Ascend 910C
PyPTO 版本/Commit: 0.2.1
CANN 版本: 9.2.0(/home/operation/Ascend/ascend-toolkit)
Python 版本: 3.11.15
操作系统: Huawei Cloud EulerOS 2.0 (aarch64)
torch 版本: 2.7.1
torch_npu 版本: 2.7.1.post2

Steps to reproduce the issue / 重现步骤 (Mandatory / 必填)

复现步骤 LSPs are disabled

 import pypto                                                                                                                                                                             
                                                                                                                                                                                          
 dest = pypto.Tensor(shape=(4, 32), dtype=pypto.DT_INT32)                                                                                                                                 
 src = pypto.Tensor(shape=(4, 16), dtype=pypto.DT_INT32)                                                                                                                                  
                                                                                                                                                                                          
 A. 漏传参数(2 个参数,@overload 要求 3 个)                                                                                                                                             
                                                                                                                                                                                          
 pypto.assemble(src, dest)                                                                                                                                                                
                                                                                                                                                                                          
 B. offsets 传 int(@overload 要求 List[...])                                                                                                                                            
                                                                                                                                                                                          
 pypto.assemble(src, 0, dest)                                                                                                                                                             
                                                                                                                                                                                          
 C. 多传参数(4 个位置参数)                                                                                                                                                              
                                                                                                                                                                                          
 pypto.assemble(src, [0, 0], dest, True)                                                                                                                                                  
                                                                                                                                                                                          
 实际输出                                                                                                                                                                                 
                                                                                                                                                                                          
 A:                                                                                                                                                                                      
                                                                                                                                                                                          
 ValueError: not enough values to unpack (expected 3, got 2)                                                                                                                              
                                                                                                                                                                                          
 B:                                                                                                                                                                                      
                                                                                                                                                                                          
 TypeError: 'int' object is not iterable                                                                                                                                                  
                                                                                                                                                                                          
 C:                                                                                                                                                                                      
                                                                                                                                                                                          
 ValueError: too many values to unpack (expected 3)                                                                                                                                       
                                                                                                                                                                                          
 三类错误均为 Python 内部解包/迭代实现细节泄露,无函数名、无签名提示、无源位置,且与 [@overload](https://gitcode.com/overload) 展示给用户的 API 形态完全脱节。                                                            

Describe the expected behavior / 预期结果 (Mandatory / 必填)

  1. @overload 仅为文档/静态类型检查,运行时无效果(operation.py:29-130) $0.00 spent

    @overload LSP
    def assemble(src: Tensor, offsets: List[Union[int, SymbolicScalar]], dst: Tensor, parallel: bool = False) -> None: LSPs are disabled
    ... # 仅类型提示

    @overload
    def assemble(srcs: Sequence[Tuple[Tensor, List[Union[int, SymbolicScalar]]]], dst: Tensor, parallel: bool = False) -> None:
    ... # 仅类型提示

    def assemble(*args, parallel: bool = False) -> None:
    # 运行时实现:无参数校验
    if isinstance(args[0], Sequence):
    srcs, dst = args # 解包失败 → 场景 A/C 的错误
    else:
    src, offsets, dst = args # 解包失败 → 场景 A/C 的错误
    pypto_impl.Assemble(src.base(), to_syms(offsets), dst.base(), parallel)
    # to_syms(0) → 'int' object is not iterable → 场景 B 的错误

    1. 两种模式的分派依赖隐式 isinstance(args[0], Sequence) 判断
    • 单源模式 (src, offsets, dst) 与多源模式 (srcs, dst) 的参数结构差异大
    • 分派完全依赖 args[0] 是否为 Sequence,与 @overload 描述的两种形态无对应关系
    • pypto.Tensor 有 iter(抛 FeError("Tensor is not iterable"))但无 len,isinstance(tensor, Sequence) 为 False(见 tensor.py:238),
      行为隐晦
    1. 错误信息无 @source_location 上下文

    operation.py 中 assemble 模块函数无 @source_location 装饰器,即使错误被 C++ 层捕获,也无法附加用户源代码位置。

  • 可同时为模块级 assemble 增加 @source_location 装饰器,与 tensor.py:731 的 Tensor.assemble 方法保持一致
    • 修复后可增加 UT:对 test_assemble.py 补充 pytest.raises(TypeError) 覆盖 A/B/C 三个场景

Special notes for this issue/备注 (Optional / 选填)

likedislike
liuxiaoxiao11liuxiaoxiao11Collaborators
Sep 9 Added label: bug-report
袁野
袁野Collaborators
Sep 9 Comments:

@seu_chang 请关注处理下,谢谢

likedislike
HuYiShengHuYiShengCollaborators
Sep 9 Associated pull request
LiuChangLiuChangCollaborators
Sep 9 Assign HuYiSheng01 as responsible person
HuYiShengHuYiShengCollaborators
Sep 11 Associated pull request
CANN-robotCANN-robotCollaborators
Sep 11 Close issue
CANN-robotCANN-robotCollaborators
Sep 11 Added label: resolved