flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

Add prefix to import path when referencing generated modules [Python, flatc 24.3.25]

Open c92s opened this issue 1 year ago • 1 comments

Imagine, we have the following schema file:

namespace mydata;

table Data {
    value:string;
    size:uint;
}

table Element {
    data:Data;
}

root_type Element;

Running flatc --python -o . example.fbs --python-typing generates a directory mydata/ with an __init__.py, a Data.py and a Element.py.

As the Element references the Data table, it imports the class from that file:

# mydata/Element.py

import flatbuffers
from flatbuffers.compat import import_numpy
from typing import Any
from mydata.Data import Data
from typing import Optional
...

Is there a way, to modify the import statement from mydata.Data import Data to, e.g. from mylibray.mydata.Data import Data, without modifying the original schema files?

c92s avatar Jun 26 '24 14:06 c92s

I did not find any direct way how to achieve this and ended up with modifying the source code of the generator to fit my needs.
¯\_(⊙~⊙)_/¯

Without that I would be screwed as I need to work with many schemes and I have not control over the namespaces within them.

If you are also that desperate, take a look at the return values of the functions GenPackageReference (line ~1105) and NestedFlatbufferType (line ~1105) in the src/idl_gen_python.cpp. I hope I didn't miss anything.

bagring avatar Aug 08 '24 14:08 bagring

This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.

github-actions[bot] avatar Feb 06 '25 20:02 github-actions[bot]

not-stale

bagring avatar Feb 12 '25 21:02 bagring

This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.

github-actions[bot] avatar Aug 14 '25 20:08 github-actions[bot]

This issue was automatically closed due to no activity for 6 months plus the 14 day notice period.

github-actions[bot] avatar Aug 29 '25 20:08 github-actions[bot]