python: decode vector-of-string fields in the object API - #9227
Open
rootkiller6788 wants to merge 2 commits into
Open
python: decode vector-of-string fields in the object API#9227rootkiller6788 wants to merge 2 commits into
rootkiller6788 wants to merge 2 commits into
Conversation
--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.
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.
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).