Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions src/idl_gen_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1092,31 +1092,26 @@ class PythonGenerator : public BaseGenerator {
code += "\n";
}

std::string NestedFlatbufferType(std::string unqualified_name) const {
StructDef *nested_root = parser_.LookupStruct(unqualified_name);
std::string qualified_name;
if (nested_root == nullptr) {
qualified_name = namer_.NamespacedType(
parser_.current_namespace_->components, unqualified_name);
// Double check qualified name just to be sure it exists.
nested_root = parser_.LookupStruct(qualified_name);
}
FLATBUFFERS_ASSERT(nested_root); // Guaranteed to exist by parser.
return qualified_name;
}

// Returns a nested flatbuffer as itself.
void GetVectorAsNestedFlatbuffer(const StructDef &struct_def,
const FieldDef &field, std::string *code_ptr,
ImportMap &imports) const {
auto nested = field.attributes.Lookup("nested_flatbuffer");
if (!nested) { return; } // There is no nested flatbuffer.

const std::string unqualified_name = nested->constant;
std::string qualified_name = NestedFlatbufferType(unqualified_name);
if (qualified_name.empty()) { qualified_name = nested->constant; }
StructDef *nested_root = parser_.LookupStruct(nested->constant);
std::string qualified_name = nested->constant;
if (nested_root == nullptr) {
qualified_name = namer_.NamespacedType(
parser_.current_namespace_->components, nested->constant);
// Double check qualified name just to be sure it exists.
nested_root = parser_.LookupStruct(qualified_name);
}
FLATBUFFERS_ASSERT(nested_root); // Guaranteed to exist by parser.

std::string unqualified_name = namer_.Type(*nested_root);

const ImportMapEntry import_entry = { qualified_name,
const ImportMapEntry import_entry = { ModuleFor(nested_root),
unqualified_name };

auto &code = *code_ptr;
Expand Down
4 changes: 2 additions & 2 deletions tests/monster_test_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ def TestnestedflatbufferAsNumpy(self):
def TestnestedflatbufferNestedRoot(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(30))
if o != 0:
from MyGame.Example.Monster import Monster
from .monster_test_generated import Monster
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think local imports with dot are a good idea

return Monster.GetRootAs(self._tab.Bytes, self._tab.Vector(o))
return 0

Expand Down Expand Up @@ -1581,7 +1581,7 @@ def TestrequirednestedflatbufferAsNumpy(self):
def TestrequirednestedflatbufferNestedRoot(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(102))
if o != 0:
from MyGame.Example.Monster import Monster
from .monster_test_generated import Monster
return Monster.GetRootAs(self._tab.Bytes, self._tab.Vector(o))
return 0

Expand Down