diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index 2ea1e8c1560..88bed850ed9 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -1092,19 +1092,6 @@ 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, @@ -1112,11 +1099,19 @@ class PythonGenerator : public BaseGenerator { 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; diff --git a/tests/monster_test_generated.py b/tests/monster_test_generated.py index 426cf9d7d3c..885d7c13ff0 100644 --- a/tests/monster_test_generated.py +++ b/tests/monster_test_generated.py @@ -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 return Monster.GetRootAs(self._tab.Bytes, self._tab.Vector(o)) return 0 @@ -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