flatbuffers/tests/MyGame/Example
Łukasz Kurowski c526cb640b
[Python] Enhance object API __init__ with typed keyword arguments (#8615)
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")
```
2025-07-22 23:57:39 -07:00
..
NestedUnion [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
__init__.py
Ability.cs
Ability.go
Ability.java
Ability.kt
Ability.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Ability.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Ability.php
Ability.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
AbilityT.java
Any.cs Fix verification for C# unions (#7970) 2023-09-30 18:54:48 -07:00
Any.go
Any.java
Any.kt
Any.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Any.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Any.php
Any.py [Python] Render enums as Python IntEnum (#8145) 2024-06-03 08:39:14 -07:00
AnyAmbiguousAliases.cs Fix verification for C# unions (#7970) 2023-09-30 18:54:48 -07:00
AnyAmbiguousAliases.go
AnyAmbiguousAliases.java
AnyAmbiguousAliases.kt
AnyAmbiguousAliases.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
AnyAmbiguousAliases.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
AnyAmbiguousAliases.php
AnyAmbiguousAliases.py [Python] Render enums as Python IntEnum (#8145) 2024-06-03 08:39:14 -07:00
AnyAmbiguousAliasesUnion.java
AnyUnion.java
AnyUniqueAliases.cs Fix verification for C# unions (#7970) 2023-09-30 18:54:48 -07:00
AnyUniqueAliases.go
AnyUniqueAliases.java
AnyUniqueAliases.kt
AnyUniqueAliases.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
AnyUniqueAliases.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
AnyUniqueAliases.php
AnyUniqueAliases.py [Python] Render enums as Python IntEnum (#8145) 2024-06-03 08:39:14 -07:00
AnyUniqueAliasesUnion.java
ArrayStruct.cs
ArrayStruct.java
ArrayStruct.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
ArrayStruct.pyi [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
ArrayStructT.java
ArrayTable.cs FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
ArrayTable.java FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
ArrayTable.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
ArrayTable.pyi [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
ArrayTableT.java
Color.cs
Color.go
Color.java
Color.kt
Color.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Color.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Color.php
Color.py
LongEnum.cs
LongEnum.go
LongEnum.java
LongEnum.kt
LongEnum.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
LongEnum.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
LongEnum.php
LongEnum.py
Monster.cs FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Monster.go
Monster.java FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Monster.kt FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Monster.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Monster.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Monster.php
Monster.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
monster_test_grpc_fb.py
MonsterStorage_grpc.go
MonsterStorageGrpc.java
MonsterT.java
NestedStruct.cs
NestedStruct.java
NestedStruct.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
NestedStruct.pyi [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
NestedStructT.java
Race.cs
Race.go
Race.java
Race.kt
Race.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Race.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Race.php
Race.py
Referrable.cs FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Referrable.go
Referrable.java FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Referrable.kt FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Referrable.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Referrable.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Referrable.php
Referrable.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
ReferrableT.java
Stat.cs FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Stat.go
Stat.java FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Stat.kt FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Stat.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Stat.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Stat.php
Stat.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
StatT.java
StructOfStructs.cs
StructOfStructs.go
StructOfStructs.java
StructOfStructs.kt
StructOfStructs.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
StructOfStructs.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
StructOfStructs.php
StructOfStructs.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
StructOfStructsOfStructs.cs
StructOfStructsOfStructs.go
StructOfStructsOfStructs.java
StructOfStructsOfStructs.kt
StructOfStructsOfStructs.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
StructOfStructsOfStructs.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
StructOfStructsOfStructs.php
StructOfStructsOfStructs.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
StructOfStructsOfStructsT.java
StructOfStructsT.java
Test.cs
Test.go
Test.java
Test.kt
Test.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Test.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Test.php
Test.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
TestEnum.cs
TestEnum.java
TestEnum.kt
TestEnum.py
TestEnum.pyi [Python] Avoid double flatbuffers include in pyi files (#8626) 2025-07-17 12:37:19 -07:00
TestSimpleTableWithEnum.cs FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
TestSimpleTableWithEnum.go
TestSimpleTableWithEnum.java FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
TestSimpleTableWithEnum.kt FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
TestSimpleTableWithEnum.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
TestSimpleTableWithEnum.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
TestSimpleTableWithEnum.php
TestSimpleTableWithEnum.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
TestSimpleTableWithEnumT.java
TestT.java
TypeAliases.cs FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
TypeAliases.go
TypeAliases.java FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
TypeAliases.kt FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
TypeAliases.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
TypeAliases.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
TypeAliases.php
TypeAliases.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
TypeAliasesT.java
Vec3.cs
Vec3.go
Vec3.java
Vec3.kt
Vec3.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Vec3.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
Vec3.php
Vec3.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
Vec3T.java