Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

thrift struct 的一个变量被命名为了 from, 在解析时未报错,但是在调用时报错了。版本是 0.3.9。 #231

Closed
j2gg0s opened this issue Aug 29, 2016 · 5 comments

Comments

@j2gg0s
Copy link

j2gg0s commented Aug 29, 2016

遇到一个问题:thrift struct 的一个变量被命名为了 from, 在解析时未报错,但是在调用时报错了。
版本是 0.3.9

具体如下:
issue.thrift 内容如下

    namespace java me.ele.ts.api

    # this is BigDecimal in java
    typedef string BigDecimal
    # This is enum in java, values are: ELEME KFPT FEORDER 

    enum SourceEnumParam {  
      ELEME = 1,  
      KFPT = 2,  
      FEORDER = 3
    } 

    struct LocationParam {
        1: BigDecimal longitude 
        2: BigDecimal latitude 
    }

    struct WalkingDistanceParam {
        1: LocationParam from 
        2: LocationParam to 
        3: string orderId 
        4: SourceEnumParam sourceEnum 
    }

issue.py 的内容如下:

    from thriftpy.parser.parser import parse
    issue_thrift = parse('issue.thrift')

    from_param = issue_thrift.LocationParam('1.23456', '2.34567')
    to_param = issue_thrift.LocationParam('3.45678', '4.56789')

    issue_thrift.WalkingDistanceParam(from_param, to_param, 'orderId', 1)

运行结果如下:

    issue git:(master) ✗ python issue.py
    Traceback (most recent call last):
      File "issue.py", line 10, in <module>
        issue_thrift.WalkingDistanceParam(from_param, to_param, 'orderId', 1)
    TypeError: object() takes no parameters
@damnever
Copy link
Contributor

damnever commented Aug 29, 2016

实际上遇到这种 compile 的时候会抛出 SyntaxError,但是被 yacc 忽略了,所以 WalkingDistanceParam 的 __init__ 实际上是没有生成成功的

#210

@lxyu
Copy link
Contributor

lxyu commented Aug 29, 2016

I think the best solution is, just avoid using python keywords as thrift struct field...

@hit9
Copy link
Contributor

hit9 commented Aug 29, 2016

Apache thrift dose not reserve from as a keyword, and it compiles the thrift above without errors. But the codes generated causes SyntaxError:

    self.from = from
    self.to = to
    self.orderId = orderId
    self.sourceEnum = sourceEnum

thriftpy parser will consistent to apache thrift's behavior. For this issue, I suggest you do not use python keywords as thrift struct fields.

@hit9
Copy link
Contributor

hit9 commented Aug 29, 2016

@damnever @JustScrapy Hi, I just checked the latest apache thrift, the from is already a reserved keyword now. PR ref

@hit9
Copy link
Contributor

hit9 commented Aug 29, 2016

Parser fixed in #232, from can not be used as an identifier any more, because from is a Python keyword.

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

No branches or pull requests

4 participants