You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@thrift.apache.org by "thomasbruggink (via GitHub)" <gi...@apache.org> on 2026/08/28 14:12:42 UTC
[PR] THRIFT-4623 Fix python complex nested struct includes [thrift]
thomasbruggink opened a new pull request, #3753:
URL: https://github.com/apache/thrift/pull/3753
<!-- Explain the changes in the pull request below: -->
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.
<!-- We recommend you review the checklist/tips before submitting a pull request. -->
- [x] Did you create an [Apache Jira](https://issues.apache.org/jira/projects/THRIFT/issues/) ticket? ([Request account here](https://selfserve.apache.org/jira-account.html), not required for trivial changes)
- [x] If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
- [x] Did you squash your changes to a single commit? (not required, but preferred)
- [x] Did you do your best to avoid breaking changes? If one was needed, did you label the Jira ticket with "Breaking-Change"?
- [x] If your change does not involve any code, include `[skip ci]` anywhere in the commit message to free up build resources.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] THRIFT-4623: Fix python complex nested struct includes [thrift]
Posted by "Jens-G (via GitHub)" <gi...@apache.org>.
Jens-G closed pull request #3753: THRIFT-4623: Fix python complex nested struct includes
URL: https://github.com/apache/thrift/pull/3753
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] THRIFT-4623: Fix python complex nested struct includes [thrift]
Posted by "Jens-G (via GitHub)" <gi...@apache.org>.
Jens-G commented on PR #3753:
URL: https://github.com/apache/thrift/pull/3753#issuecomment-6252487904
### Code review
Re-reviewed at `5513cbaf6`. No blocking issues found. Checked for bugs and CLAUDE.md compliance.
The three points from the last round are addressed.
One suggestion, below the bar for an issue but verified:
- A struct or container default is now built inside `__init__`, where every parameter shadows a module-level name. If a field has the same name as the module its default refers to, construction fails. With `include "shared.thrift"` and `struct Wrapper { 1: shared.SharedStruct shared = {} }`, this head generates
```python
def __init__(self, shared = _THRIFT_DEFAULT,):
if shared is _THRIFT_DEFAULT:
shared = shared.ttypes.SharedStruct(**{
})
```
and `Wrapper()` raises `AttributeError: 'object' object has no attribute 'ttypes'`. Reading a message that contains a `Wrapper` fails the same way, because `read()` constructs it first. `include "address.thrift"` with `1: address.Address address = {}` behaves the same. I built compilers from this head and from the merge base: the merge base's output constructs both structs and round-trips a struct that contains a `Wrapper`, this head's output fails all three. The parameter names are the keyword arguments, so the default needs a name that a field cannot shadow, for example a module-level factory per field.
https://github.com/apache/thrift/blob/5513cbaf6932f5a901196720149b9ed052771c66/compiler/cpp/src/thrift/generate/t_py_generator.cc#L914-L920
🤖 Generated with [Claude Code](https://claude.ai/code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] THRIFT-4623: Fix python complex nested struct includes [thrift]
Posted by "Jens-G (via GitHub)" <gi...@apache.org>.
Jens-G merged PR #3753:
URL: https://github.com/apache/thrift/pull/3753
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] THRIFT-4623 Fix python complex nested struct includes [thrift]
Posted by "thomasbruggink (via GitHub)" <gi...@apache.org>.
thomasbruggink commented on PR #3753:
URL: https://github.com/apache/thrift/pull/3753#issuecomment-5751481654
> @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 :bow: thanks for the review
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] THRIFT-4623 Fix python complex nested struct includes [thrift]
Posted by "Jens-G (via GitHub)" <gi...@apache.org>.
Jens-G commented on PR #3753:
URL: https://github.com/apache/thrift/pull/3753#issuecomment-5635392746
### Code review
No blocking issues found. Checked for bugs and CLAUDE.md compliance.
A few suggestions, below the bar for an issue but verified:
- A small formality for when you next push: CONTRIBUTING.md asks for a colon after the ticket id in the commit subject (`THRIFT-4623: Fix python complex nested struct includes`, the PR title can follow suit) and a `Client: py` line below it.
https://github.com/apache/thrift/blob/bb4bf37da9c565e5e5d473d6ffb3562817541a07/CONTRIBUTING.md#L18-L27
- `type` now goes through `get_true_type()`, and the same variable feeds the unchanged immutable branch below it. With `-gen py:enum`, a field typed as a typedef of an enum, in an exception or a `python.immutable` struct, now gets the enum coercion (`x if hasattr(x, 'value') else Color.__members__.get(x)`) instead of a plain assignment, so an int passed there is stored as `None`. Non-typedef enum fields already behave that way, so this may be acceptable, but it is a behaviour change outside the struct-default fix. A separate variable for the new check would avoid it.
https://github.com/apache/thrift/blob/bb4bf37da9c565e5e5d473d6ffb3562817541a07/compiler/cpp/src/thrift/generate/t_py_generator.cc#L910-L926
- With `-gen py:type_hints`, the new default produces signatures like `nested: typing.Optional[C] = _THRIFT_DEFAULT`. A type checker rejects that, because `object()` is neither `C` nor `None`. Runtime behaviour is not affected.
https://github.com/apache/thrift/blob/bb4bf37da9c565e5e5d473d6ffb3562817541a07/compiler/cpp/src/thrift/generate/t_py_generator.cc#L2786-L2794
🤖 Generated with [Claude Code](https://claude.ai/code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] THRIFT-4623 Fix python complex nested struct includes [thrift]
Posted by "Jens-G (via GitHub)" <gi...@apache.org>.
Jens-G commented on PR #3753:
URL: https://github.com/apache/thrift/pull/3753#issuecomment-5749077082
@thomasbruggink Coulkd you please have a look? Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] THRIFT-4623: Fix python complex nested struct includes [thrift]
Posted by "thomasbruggink (via GitHub)" <gi...@apache.org>.
thomasbruggink commented on PR #3753:
URL: https://github.com/apache/thrift/pull/3753#issuecomment-6093138304
@Jens-G updated, sorry for the delay :bow:
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org