Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-4623

Python compiler generates inoperable definitions for default-valued classes in same thrift file

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 0.11.0
    • Fix Version/s: None
    • Component/s: Python - Compiler
    • Labels:
      None

      Description

      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).

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                mpanthony Matt Anthony
              • Votes:
                1 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated: