Skip to content

python: decode vector-of-string fields in the object API - #9227

Open
rootkiller6788 wants to merge 2 commits into
google:masterfrom
rootkiller6788:w-flatbuffers
Open

python: decode vector-of-string fields in the object API#9227
rootkiller6788 wants to merge 2 commits into
google:masterfrom
rootkiller6788:w-flatbuffers

Conversation

@rootkiller6788

Copy link
Copy Markdown

Two small fixes to the python generator, both visible when you use the object API.

With --python-decode-obj-api-strings, UnPack() decoded a single string field but left vectors of strings as bytes, so the values came back as b'...' even though the hint says Optional[List[Optional[str]]]. The generated code now walks the vector and decodes each element, with a None check so null entries in the vector don't crash it (issue #9059). I added a tiny schema (string_vec_test.fbs) and a py_test that round-trips a [string] field through Pack/UnPack and asserts the elements come back as str.

While in there, the object stub for Pack(self, builder) was annotated -> None, but Pack() returns the uoffset int from the End call, which tripped up type checkers (issue #9057). Changed the stub to -> int and regenerated the checked-in tests/MyGame .pyi files.

I verified by building flatc and regenerating against string_vec_test.fbs, then running the new test plus the full py_test.py suite (128 tests pass).

--python-decode-obj-api-strings decoded single string fields during
UnPack but left vectors of strings as bytes, which mismatched the
Optional[List[Optional[str]]] type hint. Decode each element the same
way single strings are handled and add a small test for it.
Pack() returns the offset from EndObject()/Offset(), so the stub
saying it returns None was wrong for type checkers. Change it to int
and regenerate the checked-in .pyi files.
@github-actions github-actions Bot added python c++ codegen Involving generating code from schema labels Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant