google / google/flatbuffers

parser_.opts.python_decode_obj_api_strings doesn't apply for vector of strings

Open
#9,059 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
26.5k
Forks
3.7k
PR merge metrics
No merged PRs in 30d

Description

When a vector of strings is used, the generated code does not correctly decode into a string type and instead leaves the data as bytes (which is counter to the type hint).

Schema:

```
table test {
vec_of_string: [string];
single_string: string;
}

root_type test;
```
compiled with: `./build/flatc --python --python-version 3.12 --python-typing --gen-object-api
--python-decode-obj-api-strings -o /tmp/blah/ strings.fbs`

resulting code:

```python
class testT(object):

# testT
def __init__(self):
self.vecOfString = None # type: Optional[List[Optional[str]]]
self.singleString = None # type: Optional[str]
...
```

This is correct, though I would question if it shouldn't be `Optional[List[str]]` instead. (since the .pyi generates `typing.List[str]` not `str | None` like the single case)

```python
# testT
def _UnPack(self, test):
if test is None:
return
if not test.VecOfStringIsNone():
self.vecOfString = []
for i in range(test.VecOfStringLength()):
self.vecOfString.append(test.VecOfString(i))
self.singleString = test.SingleString()
if self.singleString is not None:
self.singleString = self.singleString.decode('utf-8')
```

This is incorrect. `self.vecOfString.append(test.VecOfString(i))` should be `self.vecOfString.append(test.VecOfString(i)).decode('utf-8')`. If it's actually optional, I guess it should do a None check first. But regardless it should be decoding to match the type hint.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.