THRIFT-4623 Fix python complex nested struct includes - #3753
Open
thomasbruggink wants to merge 1 commit into
Open
THRIFT-4623 Fix python complex nested struct includes#3753thomasbruggink wants to merge 1 commit into
thomasbruggink wants to merge 1 commit into
Conversation
Fix the bug where the following:
```
struct C {
1: i32 value
}
struct A {
1: C nested = {}
}
struct B {
1: A itm = {}
}
```
would generate the following invalid python code:
```
class C:
thrift_spec = None
def __init__(self, value=None):
self.value = value
class A:
thrift_spec = None
def __init__(self, nested=C()):
if nested is self.thrift_spec[1][4]:
nested = C()
self.nested = nested
class B:
thrift_spec = None
def __init__(self, itm=A()):
if itm is self.thrift_spec[1][4]:
itm = A()
self.itm = itm
```
The problem with the above code is that `self.thrift_spec` was initialized and set after the module was loaded.
In python this would work when B is instantiated but since B immediately
invokes A() it happens before the thrift_spec is set and this would
result in `TypeError: 'NoneType' object is not subscriptable`.
By first initializing a temporary placeholder with a unique address to
compare to called `_THRIFT_DEFAULT` the generated code would look like:
```
_THRIFT_DEFAULT = object()
class C:
def __init__(self, value=None):
self.value = value
class A:
def __init__(self, nested=_THRIFT_DEFAULT):
if nested is _THRIFT_DEFAULT:
nested = C()
class B:
def __init__(self, itm=_THRIFT_DEFAULT):
if itm is _THRIFT_DEFAULT:
itm = A()
```
Here the instantiation of the default object is defered until after the
module has been fully loaded resulting in the correct value being
assigned.
thomasbruggink
force-pushed
the
fix-recursive-complex-python-includes
branch
from
August 28, 2026 14:17
8049ffd to
bb4bf37
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix the bug where the following:
would generate the following invalid python code:
The problem with the above code is that
self.thrift_specwas initialized and set after the module was loaded. In python this would work when B is instantiated but since B immediately invokes A() it happens before the thrift_spec is set and this would result inTypeError: 'NoneType' object is not subscriptable. By first initializing a temporary placeholder with a unique address to compare to called_THRIFT_DEFAULTthe generated code would look like:Here the instantiation of the default object is defered until after the module has been fully loaded resulting in the correct value being assigned.
[skip ci]anywhere in the commit message to free up build resources.