Skip to content

THRIFT-4623 Fix python complex nested struct includes - #3753

Open
thomasbruggink wants to merge 1 commit into
apache:masterfrom
thomasbruggink:fix-recursive-complex-python-includes
Open

THRIFT-4623 Fix python complex nested struct includes#3753
thomasbruggink wants to merge 1 commit into
apache:masterfrom
thomasbruggink:fix-recursive-complex-python-includes

Conversation

@thomasbruggink

Copy link
Copy Markdown
Contributor

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.

  • Did you create an Apache Jira ticket? (Request account here, not required for trivial changes)
  • If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
  • Did you squash your changes to a single commit? (not required, but preferred)
  • Did you do your best to avoid breaking changes? If one was needed, did you label the Jira ticket with "Breaking-Change"?
  • If your change does not involve any code, include [skip ci] anywhere in the commit message to free up build resources.

@mergeable mergeable Bot added python compiler build and general CI cmake, automake and build system changes labels Aug 28, 2026
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
thomasbruggink force-pushed the fix-recursive-complex-python-includes branch from 8049ffd to bb4bf37 Compare August 28, 2026 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build and general CI cmake, automake and build system changes compiler python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant