[ 
https://issues.apache.org/jira/browse/THRIFT-4623?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16592717#comment-16592717
 ] 

Matt Anthony commented on THRIFT-4623:
--------------------------------------

Would appreciate it. The workaround I found involved making a temp copy of the 
thrift and removing the offending elements which is a total hack. The Python 
that the compiler generates is very much flawed ... as I mentioned, the 
__init__ methods reference self.thrift_spec, which is not defined within the 
class or prior to it being referenced. For some reason that class element is 
defined outside of the class definition, at the very end of the compiled thrift 
files. This is not just bad python, it's just generally bad programming to 
reference an object in a class initialization that you don't define within the 
definition. Is there a reason that thrift_spec can't be defined as a class 
variable for the class at the time the class definition is written? It would 
seem like that must already be known at that time if the rest of the class 
definition is written correctly.

> Python compiler generates inoperable definitions for default-valued classes 
> in same thrift file
> -----------------------------------------------------------------------------------------------
>
>                 Key: THRIFT-4623
>                 URL: https://issues.apache.org/jira/browse/THRIFT-4623
>             Project: Thrift
>          Issue Type: Bug
>          Components: Python - Compiler
>    Affects Versions: 0.11.0
>            Reporter: Matt Anthony
>            Priority: Major
>
> Consider a thrift file as
>  
> <code>
> struct A {
>     i32 value;
> }
>  
> struct B {
>     A itm = {}
> }
> </code>
>  
> The thrift compiler will generate class definitions for A and B, without 
> `self.thrift_spec` defined, and at the end of the ttypes.py file will go back 
> and populate the thrift_spec objects. However, in the __init__ method of B, 
> the default value for `itm` would attempt to be set as `A(**{})`, which will 
> crash with error `AttributeError: 'ObservationInfo' object has no attribute 
> 'thrift_spec'`. This makes sense since at the time the default object is 
> being established, there is no thrift_spec object.
> Removing the default value removes the error, but this is not correct 
> behavior. The most obvious fix is that the `thrift_spec` object of each class 
> needs to be defined within the class as the class is written, not at the end 
> of the file after all the class objects are established. Moreover, that is 
> generally good practice and is actually part of the PEP prescriptions for 
> python (referencing objects before initialization in a class is a highlighted 
> warning, for this very reason).



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to