Thriftpy / thriftpy2

Pure python approach of Apache Thrift.
MIT License
562 stars 89 forks source link

thriftpy2.load fail #279

Open lockeliu opened 1 month ago

lockeliu commented 1 month ago

我的thrift 文件如下: `namespace go test.test.test namespace py cg

struct Template { 1: optional string async 2: optional Test test

}

struct Test { 1: required string name } `

main函数如下:

`import os import sys import thriftpy2

if name == "main": thriftpy2.parser.lexer.thrift_reserved_keywords = [] IDL_PATH="./test.thrift" res = thriftpy2.load(IDL_PATH, module_name="cg_thrift")`

失败信息如下: thriftpy2.parser.exc.ThriftParserError: No type found: 'Test', at line 6

如果把thrift 里面的 async 字段去掉就成功了

aisk commented 1 month ago

async is a keyword in Python, so we can't generate a Python class with this field name. But maybe we can improve the error message for better explanation.

lockeliu commented 1 month ago

async is a keyword in Python, so we can't generate a Python class with this field name. But maybe we can improve the error message for better explanation.

If I put the test structure before Template, will it succeed again? For example `namespace go test.test.test namespace py cg

struct Test { 1: required string name }

struct Template { 1: optional string async 2: optional Test test

}

`

Is there any way to solve this problem without changing the field name?

aisk commented 1 month ago

We can load the thrift file in the second case, but the loaded types are still incomplete, it wouldn't work as you expected. We should raise an error in this case.

For your specified scenario, you can change the field name async to another one. The Thrift protocol only uses position as the field name, so it's okay to use a different field name between the servers and clients.

lockeliu commented 1 month ago

Got it, thanks.

lockeliu commented 1 month ago

Are there any other python keywords that cannot be generated?

aisk commented 1 month ago

Any keyword should not be used here.