Skip to content

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

@dshekter

Description

@dshekter

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:

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)

    # 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions