Skip to content

Commit c7b9dc8

Browse files
authored
[Python] Avoid include own type (#8625)
This prevents the include of the type defined in the pyi, otherwise this leads to error message like this: error: Name XYZ already defined (possibly by an import) [no-redef]
1 parent 4c9079e commit c7b9dc8

File tree

12 files changed

+45
-16
lines changed

12 files changed

+45
-16
lines changed

include/codegen/python.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,21 @@ const python::Import &python::Imports::Import(const std::string &module,
5959
imports.push_back(std::move(import));
6060
return imports.back();
6161
}
62+
63+
const python::Import &python::Imports::Export(const std::string &module) {
64+
python::Import import;
65+
import.module = module;
66+
exports.push_back(std::move(import));
67+
return exports.back();
68+
}
69+
70+
const python::Import &python::Imports::Export(const std::string &module,
71+
const std::string &name) {
72+
python::Import import;
73+
import.module = module;
74+
import.name = name;
75+
exports.push_back(std::move(import));
76+
return exports.back();
77+
}
6278
} // namespace python
6379
} // namespace flatbuffers

include/codegen/python.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ struct Imports {
8585
const python::Import &Import(const std::string &module,
8686
const std::string &name);
8787

88+
const python::Import &Export(const std::string &module);
89+
const python::Import &Export(const std::string &module,
90+
const std::string &name);
91+
8892
std::vector<python::Import> imports;
93+
std::vector<python::Import> exports;
8994
};
9095
} // namespace python
9196
} // namespace flatbuffers

src/idl_gen_python.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/MyGame/Example/ArrayStruct.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import numpy as np
55

66
import flatbuffers
77
import typing
8-
from MyGame.Example.ArrayStruct import ArrayStruct
98
from MyGame.Example.NestedStruct import NestedStruct, NestedStructT
109
from MyGame.Example.TestEnum import TestEnum
1110

tests/MyGame/Example/ArrayTable.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import numpy as np
66
import flatbuffers
77
import typing
88
from MyGame.Example.ArrayStruct import ArrayStruct, ArrayStructT
9-
from MyGame.Example.ArrayTable import ArrayTable
109

1110
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
1211

tests/MyGame/Example/NestedStruct.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import numpy as np
55

66
import flatbuffers
77
import typing
8-
from MyGame.Example.NestedStruct import NestedStruct
98
from MyGame.Example.TestEnum import TestEnum
109

1110
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type

tests/MyGame/Example/NestedUnion/Any.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import numpy as np
55

66
import flatbuffers
77
import typing
8-
from MyGame.Example.NestedUnion.Any import Any
98
from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnum
109
from MyGame.Example.NestedUnion.Vec3 import Vec3
1110
from flatbuffers import table

tests/MyGame/Example/NestedUnion/NestedUnionTest.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import numpy as np
66
import flatbuffers
77
import typing
88
from MyGame.Example.NestedUnion.Any import Any
9-
from MyGame.Example.NestedUnion.NestedUnionTest import NestedUnionTest
109
from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnumT
1110
from MyGame.Example.NestedUnion.Vec3 import Vec3T
1211
from flatbuffers import table

tests/MyGame/Example/NestedUnion/Test.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import numpy as np
55

66
import flatbuffers
77
import typing
8-
from MyGame.Example.NestedUnion.Test import Test
98

109
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
1110

tests/MyGame/Example/NestedUnion/TestSimpleTableWithEnum.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import numpy as np
66
import flatbuffers
77
import typing
88
from MyGame.Example.NestedUnion.Color import Color
9-
from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnum
109

1110
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
1211

0 commit comments

Comments
 (0)