Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thriftpy2.load fail #279

Open
lockeliu opened this issue Jun 22, 2024 · 6 comments
Open

thriftpy2.load fail #279

lockeliu opened this issue Jun 22, 2024 · 6 comments

Comments

@lockeliu
Copy link

我的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
Copy link
Member

aisk commented Jun 22, 2024

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
Copy link
Author

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
Copy link
Member

aisk commented Jun 23, 2024

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
Copy link
Author

Got it, thanks.

@lockeliu
Copy link
Author

Are there any other python keywords that cannot be generated?

@aisk
Copy link
Member

aisk commented Jun 24, 2024

Any keyword should not be used here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants