You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pythran中正确标注字典列表的参数类型?

问题:Pythran中字典序列的正确类型标注方式

在纯Python文件中调用Pythran编译的dictest函数时,需传入字典组成的序列(示例中为ll)。当前在Pythran函数定义文件test.py中使用#pythran export dictest(str:float dict list )做类型标注,编译时出现大量错误,确认错误源于字典序列的类型标注方式,求正确标注方法。


相关代码示例

纯Python调用代码

import test_pythran as tp

dd = {
        "ux": 10.0,
        "uy": 5,
        "uz": 3.4
        }

ee = {
        "ux": 11.0,
        "uy": 7,
        "uz": 2.4
        }

ll = (dd,ee)

tp.dictest(ll)

Pythran函数定义(原错误版本)

#pythran export dictest(str:float dict list )

def dictest(ll):
    print(ll["ux"], ll["uy"], ll["uz"])

编译命令

pythran test.py -o test_pythran.so

编译报错信息

WARNING: Compilation error, trying hard to find its origin...
Compilation error, trying hard to find its origin...
WARNING: Nope, I'm going to flood you with C++ errors!
Nope, I'm going to flood you with C++ errors!
CRITICAL: Cover me Jack. Jack? Jaaaaack!!!!
E: error: Command "/usr/local/opt/gcc/bin/g++-12 -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -DENABLE_PYTHON_MODULE -D__PYTHRAN__=3 -DPYTHRAN_BLAS_OPENBLAS -I/usr/local/opt/openblas/include -I/usr/local/Cellar/pythran/0.12.0/libexec/lib/python3.10/site-packages/pythran -I/usr/local/opt/numpy/lib/python3.10/site-packages/numpy/core/include -I/usr/local/Cellar/pythran/0.12.0/libexec/include -I/usr/local/opt/python@3.10/Frameworks/Python.framework/Versions/3.10/include/python3.10 -c /var/folders/41/dj_cjw2977qbd4fqxf1c79tc0000gn/T/tmpctvfroxf.cpp -o /var/folders/41/dj_cjw2977qbd4fqxf1c79tc0000gn/T/tmp3ae2neiv/var/folders/41/dj_cjw2977qbd4fqxf1c79tc0000gn/T/tmpctvfroxf.o -std=c++11 -fno-math-errno -Wno-unused-function" failed with exit status 1
Cover me Jack. Jack? Jaaaaack!!!!
E: error: Command "/usr/local/opt/gcc/bin/g++-12 -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -DENABLE_PYTHON_MODULE -D__PYTHRAN__=3 -DPYTHRAN_BLAS_OPENBLAS -I/usr/local/opt/openblas/include -I/usr/local/Cellar/pythran/0.12.0/libexec/lib/python3.10/site-packages/pythran -I/usr/local/opt/numpy/lib/python3.10/site-packages/numpy/core/include -I/usr/local/Cellar/pythran/0.12.0/libexec/include -I/usr/local/opt/python@3.10/Frameworks/Python.framework/Versions/3.10/include/python3.10 -c /var/folders/41/dj_cjw2977qbd4fqxf1c79tc0000gn/T/tmpctvfroxf.cpp -o /var/folders/41/dj_cjw2977qbd4fqxf1c79tc0000gn/T/tmp3ae2neiv/var/folders/41/dj_cjw2977qbd4fqxf1c79tc0000gn/T/tmpctvfroxf.o -std=c++11 -fno-math-errno -Wno-unused-function" failed with exit status 1

解决方案

1. 修正类型标注格式

Pythran的类型标注遵循容器[元素类型]的嵌套格式,字典序列的正确标注方式为:

#pythran export dictest(list[dict[str, float]])

如果传入的是元组序列,也可以标注为tuple[dict[str, float]],Pythran通常会自动兼容序列类型,用list标注也能处理元组输入。

2. 修正函数逻辑错误

原函数中直接对序列ll使用ll["ux"]是错误的——序列是按索引访问,而非键。需要遍历序列中的每个字典:

#pythran export dictest(list[dict[str, float]])

def dictest(ll):
    for d in ll:
        print(d["ux"], d["uy"], d["uz"])

3. 补充说明

  • 原标注str:float dict list不符合Pythran的类型语法规则,导致编译解析失败。
  • Pythran中字典的类型标注格式为dict[键类型, 值类型],再嵌套进列表/元组等容器类型中。

内容的提问来源于stack exchange,提问作者Martin R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 20:55:10