@@ -290,6 +290,7 @@ class PythonStubGenerator {
290290 void GenerateObjectStub (std::stringstream &stub, const StructDef *struct_def,
291291 Imports *imports) const {
292292 std::string name = namer_.ObjectType (*struct_def);
293+ imports->Export (ModuleFor (struct_def), namer_.Type (*struct_def));
293294
294295 stub << " class " << name;
295296 if (version_.major != 3 ) stub << " (object)" ;
@@ -327,6 +328,7 @@ class PythonStubGenerator {
327328 void GenerateStructStub (std::stringstream &stub, const StructDef *struct_def,
328329 Imports *imports) const {
329330 std::string type = namer_.Type (*struct_def);
331+ imports->Export (ModuleFor (struct_def), namer_.Type (*struct_def));
330332
331333 stub << " class " << type;
332334 if (version_.major != 3 ) stub << " (object)" ;
@@ -545,6 +547,7 @@ class PythonStubGenerator {
545547 void GenerateEnumStub (std::stringstream &stub, const EnumDef *enum_def,
546548 Imports *imports) const {
547549 stub << " class " << namer_.Type (*enum_def);
550+ imports->Export (ModuleFor (enum_def), namer_.Type (*enum_def));
548551
549552 if (version_.major == 3 ){
550553 imports->Import (" enum" , " IntEnum" );
@@ -589,18 +592,32 @@ class PythonStubGenerator {
589592 }
590593 }
591594
595+ // Remove imports from exports
596+ for (const Import &import : imports.exports ) {
597+ if (import .name == " " ) {
598+ modules.erase (import .module );
599+ } else {
600+ auto search = names_by_module.find (import .module );
601+ if (search != names_by_module.end ()) {
602+ search->second .erase (import .name );
603+ }
604+ }
605+ }
606+
592607 for (const std::string &module : modules) {
593608 ss << " import " << module << ' \n ' ;
594609 }
595610 for (const auto &import : names_by_module) {
596- ss << " from " << import .first << " import " ;
597- size_t i = 0 ;
598- for (const std::string &name : import .second ) {
599- if (i > 0 ) ss << " , " ;
600- ss << name;
601- ++i;
611+ if (!import .second .empty ()) {
612+ ss << " from " << import .first << " import " ;
613+ size_t i = 0 ;
614+ for (const std::string &name : import .second ) {
615+ if (i > 0 ) ss << " , " ;
616+ ss << name;
617+ ++i;
618+ }
619+ ss << ' \n ' ;
602620 }
603- ss << ' \n ' ;
604621 }
605622 }
606623
0 commit comments