Repository navigation
THRIFT-4623: Fix python complex nested struct includes - #3753
Conversation
8049ffd to
bb4bf37
Compare
Code reviewNo blocking issues found. Checked for bugs and CLAUDE.md compliance. A few suggestions, below the bar for an issue but verified:
Lines 18 to 27 in bb4bf37
thrift/compiler/cpp/src/thrift/generate/t_py_generator.cc Lines 910 to 926 in bb4bf37
thrift/compiler/cpp/src/thrift/generate/t_py_generator.cc Lines 2786 to 2794 in bb4bf37 🤖 Generated with Claude Code |
|
@thomasbruggink Coulkd you please have a look? Thanks! |
@Jens-G i can take a look at the end of next week, currently on vacation without access to a computer 🙇 thanks for the review |
Client: py
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.
bb4bf37 to
5513cba
Compare
|
@Jens-G updated, sorry for the delay 🙇 |
Code reviewRe-reviewed at The three points from the last round are addressed. One suggestion, below the bar for an issue but verified:
thrift/compiler/cpp/src/thrift/generate/t_py_generator.cc Lines 914 to 920 in 5513cba 🤖 Generated with Claude Code |
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.