flatbuffers/tests
Ł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
..
64bit FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
annotated_binary Fixed vtable duplication for binary annotator (#7809) 2023-02-02 10:39:39 -08:00
arrays_test Removes 'size' and 'alignment' as Rust keywords (#8139) 2024-11-12 05:13:02 +00:00
bazel_repository_test_dir Remove Bazel WORKSPACE setup. (#8509) 2025-01-24 18:16:10 +00:00
cpp17 FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
DictionaryLookup FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
docker
evolution_test FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
FlatBuffers.Benchmarks
FlatBuffers.Test Remove Resource path 2025-01-09 21:30:01 -08:00
flatc [Kotlin] Control the generation of reflection with --reflect-names (#7775) 2023-01-10 10:03:39 -08:00
fuzzer Rename GenerateText 2023-05-11 18:14:27 -07:00
include_test
include_test1 Removes 'size' and 'alignment' as Rust keywords (#8139) 2024-11-12 05:13:02 +00:00
include_test2 Removes 'size' and 'alignment' as Rust keywords (#8139) 2024-11-12 05:13:02 +00:00
key_field FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
keyword_test [Rust] Add the Allocator trait for the builder API (#8106) 2023-10-07 15:28:52 -04:00
KeywordTest FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
minified_enums FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
monster_test Rust reflection: simplify dependencies, fix Android build compatibility (#8512) 2025-02-04 08:40:31 -08:00
monster_test_serialize Rust reflection: simplify dependencies, fix Android build compatibility (#8512) 2025-02-04 08:40:31 -08:00
monster_test_suffix FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
more_defaults [Rust] Add the Allocator trait for the builder API (#8106) 2023-10-07 15:28:52 -04:00
MyGame [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
name_clash_test
namespace_test FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
nested_namespace_test FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
nim
optional_scalars [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
order
private_annotation_test Removes 'size' and 'alignment' as Rust keywords (#8139) 2024-11-12 05:13:02 +00:00
prototest Parsing from proto should keep field ID. (fixes #7645) (#7655) 2023-02-01 11:17:35 -08:00
rust_namer_test Removes 'size' and 'alignment' as Rust keywords (#8139) 2024-11-12 05:13:02 +00:00
rust_no_std_compilation_test
rust_reflection_test Rust full reflection (#8102) 2025-01-15 10:03:10 -08:00
rust_serialize_test
rust_usage_test Rust reflection: simplify dependencies, fix Android build compatibility (#8512) 2025-02-04 08:40:31 -08:00
swift [Swift] Memory usage fix (#8643) 2025-07-18 09:37:58 -07:00
ts [TS] Fix relative paths for exports (#8517) 2025-07-01 08:38:02 -07:00
type_field_collsion FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
union_value_collsion FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
union_vector FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
.gitignore remove and auto-generate alignment_test_generated.h 2023-05-12 16:26:21 -07:00
Abc.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
alignment_test.cpp Add a unit test for odd-sized small structs (for #8117) (#8363) 2024-08-20 00:27:32 -04:00
alignment_test.fbs Add a unit test for odd-sized small structs (for #8117) (#8363) 2024-08-20 00:27:32 -04:00
alignment_test.h
alignment_test.json Fix misalignment of small structs in a Vector (C++) (#7883) 2023-09-28 22:53:50 -07:00
alignment_test_after_fix.afb Fix misalignment of small structs in a Vector (C++) (#7883) 2023-09-28 22:53:50 -07:00
alignment_test_after_fix.bin Fix misalignment of small structs in a Vector (C++) (#7883) 2023-09-28 22:53:50 -07:00
alignment_test_before_fix.afb Fix misalignment of small structs in a Vector (C++) (#7883) 2023-09-28 22:53:50 -07:00
alignment_test_before_fix.bin Fix misalignment of small structs in a Vector (C++) (#7883) 2023-09-28 22:53:50 -07:00
arrays_test.bfbs
arrays_test.fbs
arrays_test.golden
arrays_test.schema.json
BUILD.bazel Test external modules explicitly in CI (#8507) 2025-01-23 23:04:06 +00:00
DartTest.sh dart: use enhanced enums (#8313) 2024-11-18 17:31:19 +00:00
dictionary_lookup.fbs
evolution_test.cpp feat(C++): Support underlying_type for union (#7954) 2023-05-14 21:22:38 -07:00
evolution_test.h
flexbuffers_test.cpp FlexBuffers: JSON output supports indentation 2023-06-14 18:12:55 -07:00
flexbuffers_test.h
fuzz_test.cpp Rename GenerateText 2023-05-11 18:14:27 -07:00
fuzz_test.h
go_test.go Support file_identifier in Go (#7904) 2023-04-26 05:15:09 +00:00
gold_flexbuffer_example.bin
GoTest.sh
include_build_test.cc
include_test1_generated.dart dart: use enhanced enums (#8313) 2024-11-18 17:31:19 +00:00
include_test2_my_game.other_name_space_generated.dart dart: use enhanced enums (#8313) 2024-11-18 17:31:19 +00:00
included_test.fbs
includer_test.fbs
is_quiet_nan.h
javatest.bin
json_test.cpp Rename GenerateText 2023-05-11 18:14:27 -07:00
json_test.h Make JSON supporting advanced union features (#7869) 2023-04-26 05:37:06 +00:00
key_field_test.cpp run scripts/clang-format-all.sh 2023-05-11 12:23:49 -07:00
key_field_test.h [C++] Enable using struct and array of struct as key (#7741) 2023-01-24 16:37:13 -08:00
keyword_test.fbs
KotlinTest.kt
KotlinTest.sh Add Kotlin multiplatform support (#7969) 2023-05-26 11:00:33 -07:00
LobsterTest.bat Lobster namespace change 2023-09-17 10:21:58 -07:00
lobstertest.lobster corrected a typo (#8063) 2023-09-29 07:40:02 -07:00
long_namespace.fbs [TS] Fix relative paths for exports (#8517) 2025-07-01 08:38:02 -07:00
longer_namespace.fbs [TS] Fix relative paths for exports (#8517) 2025-07-01 08:38:02 -07:00
LuaTest.bat
luatest.lua
LuaTest.sh
monster_extra.fbs
monster_extra_generated.h FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
monster_extra_my_game_generated.dart dart: use enhanced enums (#8313) 2024-11-18 17:31:19 +00:00
monster_test.afb Fixed vtable duplication for binary annotator (#7809) 2023-02-02 10:39:39 -08:00
monster_test.bfbs
monster_test.cpp Fix handling non null-terminated string_views in LookupByKey (#8203) 2024-03-25 10:39:51 -07:00
monster_test.fbs
monster_test.grpc.fb.cc
monster_test.grpc.fb.h
monster_test.h
monster_test.schema.json
monster_test_bfbs_generated.h don't emit flatbuffers include in bfbs generated output (#7968) 2023-05-17 21:13:16 -07:00
monster_test_generated.grpc.fb.cc
monster_test_generated.grpc.fb.h
monster_test_generated.h FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
monster_test_generated.lobster Lobster namespace change 2023-09-17 10:21:58 -07:00
monster_test_generated.py [Python] Enhance object API __init__ with typed keyword arguments (#8615) 2025-07-22 23:57:39 -07:00
monster_test_generated.ts
monster_test_my_game.example2_generated.dart dart: use enhanced enums (#8313) 2024-11-18 17:31:19 +00:00
monster_test_my_game.example_generated.dart dart: use enhanced enums (#8313) 2024-11-18 17:31:19 +00:00
monster_test_my_game_generated.dart dart: use enhanced enums (#8313) 2024-11-18 17:31:19 +00:00
monsterdata_extra.json
monsterdata_go_wire.mon.sp Support file_identifier in Go (#7904) 2023-04-26 05:15:09 +00:00
monsterdata_python_wire.mon
monsterdata_test.afb Fixed vtable duplication for binary annotator (#7809) 2023-02-02 10:39:39 -08:00
monsterdata_test.golden
monsterdata_test.json
monsterdata_test.mon
more_defaults.fbs
MoreDefaults.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
MutatingBool.fbs
nan_inf_test.fbs
native_inline_table_test.fbs
native_inline_table_test_generated.h FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
native_type_test.fbs
native_type_test_generated.h FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
native_type_test_impl.cpp
native_type_test_impl.h
nested_union_test.fbs
optional_scalars.fbs
optional_scalars.json
optional_scalars_defaults.json
optional_scalars_generated.h FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
optional_scalars_generated.lobster Lobster namespace change 2023-09-17 10:21:58 -07:00
optional_scalars_generated.ts
optional_scalars_test.cpp run scripts/clang-format-all.sh 2023-05-11 12:23:49 -07:00
optional_scalars_test.h
parser_test.cpp feat: Support union underlying type for TS/JS (#7961) 2023-05-15 13:18:49 -07:00
parser_test.h
phpTest.php
phpUnionVectorTest.php
phpUnionVectorTest.sh
Pizza.go
private_annotation_test.fbs
Property.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
proto_test.cpp Rename GenerateText 2023-05-11 18:14:27 -07:00
proto_test.h Parsing from proto should keep field ID. (fixes #7645) (#7655) 2023-02-01 11:17:35 -08:00
py_flexbuffers_test.py Fix Python host-endianness dependencies (#7773) 2023-01-10 10:20:08 -08:00
py_test.py Add Clear() for python Builder (#8186) 2023-12-17 22:18:11 -08:00
PythonTest.sh [gRPC] Update the code generator for Python to produce typed handlers (#8326) 2024-06-18 16:02:57 -07:00
reflection_test.cpp various fixes (#7986) 2023-05-31 11:52:05 -07:00
reflection_test.h
required_strings.fbs [TS]: builder, Fix requiredField(). Verity that the field is present in the vtable (#7739) (#7752) 2023-01-10 10:43:17 -08:00
rust_namer_test.fbs Removes 'size' and 'alignment' as Rust keywords (#8139) 2024-11-12 05:13:02 +00:00
RustTest.bat chore: add rust windows test to CI, fix RustTest.bat (#7963) 2023-05-16 16:06:41 -07:00
RustTest.sh
service_test.fbs [gRPC] Update the code generator for Python to produce typed handlers (#8326) 2024-06-18 16:02:57 -07:00
service_test_generated.py [gRPC] Update the code generator for Python to produce typed handlers (#8326) 2024-06-18 16:02:57 -07:00
service_test_generated.pyi [gRPC] Update the code generator for Python to produce typed handlers (#8326) 2024-06-18 16:02:57 -07:00
service_test_grpc.fb.py [gRPC] Update the code generator for Python to produce typed handlers (#8326) 2024-06-18 16:02:57 -07:00
service_test_grpc.fb.pyi [gRPC] Update the code generator for Python to produce typed handlers (#8326) 2024-06-18 16:02:57 -07:00
TableA.lua FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
TableA.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
TableAT.java
test.cpp Fix dereference operator of VectorIterator to structures (#8425) 2025-05-17 22:01:09 -07:00
test.fbs Optionally generate Python type annotations (#7858) 2023-04-28 09:38:29 -07:00
test_assert.cpp
test_assert.h moved function to namespace (#8068) 2023-09-29 07:50:06 -07:00
test_builder.cpp run scripts/clang-format-all.sh 2023-05-11 12:23:49 -07:00
test_builder.h FlatBuffers 64 for C++ (#7935) 2023-05-09 09:16:30 -07:00
TestAll.sh [TS] Allows object API to set 0 for a null-default scalar. (#7864) 2023-09-12 19:58:55 +02:00
TestMutatingBool.nim FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
type_field_collsion.fbs
unicode_test.json
unicode_test.mon
union_underlying_type_test.fbs feat(C++): Support underlying_type for union (#7954) 2023-05-14 21:22:38 -07:00
union_underlying_type_test_generated.h FlatBuffers Version 25.2.10 2025-02-10 20:25:03 -08:00
union_value_collision.fbs [CS] Naming collision if field has same name as table and used as key (#7842) 2023-03-02 21:42:27 -08:00
util_test.cpp ToCamelCase() when kLowerCamel now converts first char to lower. (#7838) 2023-03-15 01:09:24 +00:00
util_test.h
vector_has_test.fbs