[Python] Enhance object API __init__ with typed keyword arguments#8615
Merged
aardappel merged 1 commit intogoogle:masterfrom Jul 23, 2025
Merged
[Python] Enhance object API __init__ with typed keyword arguments#8615aardappel merged 1 commit intogoogle:masterfrom
__init__ with typed keyword arguments#8615aardappel merged 1 commit intogoogle:masterfrom
Conversation
3df6969 to
2043352
Compare
Contributor
|
Nice, but im almost certain this will break Python 2 support Some guarding against that would be probably useful |
akb825
reviewed
Jun 26, 2025
45a426b to
0455d8c
Compare
crackcomm
commented
Jul 14, 2025
crackcomm
commented
Jul 14, 2025
This commit significantly improves the developer experience for the Python Object-Based API by overhauling the generated `__init__` method for `T`-suffixed classes.
Previously, `T` objects had to be instantiated with an empty constructor, and their fields had to be populated manually one by one. This was verbose and not idiomatic Python.
This change modifies the Python code generator (`GenInitialize`) to produce `__init__` methods that are:
1. **Keyword-Argument-Friendly**: The constructor now accepts all table/struct fields as keyword arguments, allowing for concise, single-line object creation.
2. **Fully Typed**: The signature of the `__init__` method is now annotated with Python type hints. This provides immediate benefits for static analysis tools (like Mypy) and IDEs, enabling better autocompletion and type checking.
3. **Correctly Optional**: The generator now correctly wraps types in `Optional[...]` if their default value is `None`. This applies to strings, vectors, and other nullable fields, ensuring strict type safety.
The new approach remains **fully backward-compatible**, as all arguments have default values. Existing code that uses the empty constructor will continue to work without modification.
#### Example of a Generated `__init__`
**Before:**
```python
class KeyValueT(object):
def __init__(self):
self.key = None # type: str
self.value = None # type: str
```
**After:**
```python
class KeyValueT(object):
def __init__(self, key: Optional[str] = None, value: Optional[str] = None):
self.key = key
self.value = value
```
#### Example of User Code
**Before:**
```python
# Old, verbose way
kv = KeyValueT()
kv.key = "instrument"
kv.value = "EUR/USD"
```
**After:**
```python
# New, Pythonic way
kv = KeyValueT(key="instrument", value="EUR/USD")
```
0455d8c to
c588585
Compare
Contributor
Author
|
The code was generated with |
Contributor
I guess this flag should not be part of |
Contributor
|
@aardappel I just tested this and it looks good to me 👍 if @akb825 has no more comments i guess this could be merged? |
Contributor
|
@aardappel i guess this can be merged? |
Contributor
Sorry, I forgot to reply earlier, looks good on my end. |
aardappel
approved these changes
Jul 23, 2025
Collaborator
|
thanks :) |
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.
This commit significantly improves the developer experience for the Python Object-Based API by overhauling the generated
__init__method forT-suffixed classes.Previously,
Tobjects had to be instantiated with an empty constructor, and their fields had to be populated manually one by one. This was verbose and not idiomatic Python.This change modifies the Python code generator (
GenInitialize) to produce__init__methods that are:Keyword-Argument-Friendly: The constructor now accepts all table/struct fields as keyword arguments, allowing for concise, single-line object creation.
Fully Typed: The signature of the
__init__method is now annotated with Python type hints. This provides immediate benefits for static analysis tools (like Mypy) and IDEs, enabling better autocompletion and type checking.Correctly Optional: The generator now correctly wraps types in
Optional[...]if their default value isNone. This applies to strings, vectors, and other nullable fields, ensuring strict type safety.The new approach remains fully backward-compatible, as all arguments have default values. Existing code that uses the empty constructor will continue to work without modification.
Example of a Generated
__init__Before:
After:
Example of User Code
Before:
After: