mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
[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]
This commit is contained in:
@@ -59,5 +59,21 @@ const python::Import &python::Imports::Import(const std::string &module,
|
|||||||
imports.push_back(std::move(import));
|
imports.push_back(std::move(import));
|
||||||
return imports.back();
|
return imports.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const python::Import &python::Imports::Export(const std::string &module) {
|
||||||
|
python::Import import;
|
||||||
|
import.module = module;
|
||||||
|
exports.push_back(std::move(import));
|
||||||
|
return exports.back();
|
||||||
|
}
|
||||||
|
|
||||||
|
const python::Import &python::Imports::Export(const std::string &module,
|
||||||
|
const std::string &name) {
|
||||||
|
python::Import import;
|
||||||
|
import.module = module;
|
||||||
|
import.name = name;
|
||||||
|
exports.push_back(std::move(import));
|
||||||
|
return exports.back();
|
||||||
|
}
|
||||||
} // namespace python
|
} // namespace python
|
||||||
} // namespace flatbuffers
|
} // namespace flatbuffers
|
||||||
|
|||||||
@@ -85,7 +85,12 @@ struct Imports {
|
|||||||
const python::Import &Import(const std::string &module,
|
const python::Import &Import(const std::string &module,
|
||||||
const std::string &name);
|
const std::string &name);
|
||||||
|
|
||||||
|
const python::Import &Export(const std::string &module);
|
||||||
|
const python::Import &Export(const std::string &module,
|
||||||
|
const std::string &name);
|
||||||
|
|
||||||
std::vector<python::Import> imports;
|
std::vector<python::Import> imports;
|
||||||
|
std::vector<python::Import> exports;
|
||||||
};
|
};
|
||||||
} // namespace python
|
} // namespace python
|
||||||
} // namespace flatbuffers
|
} // namespace flatbuffers
|
||||||
|
|||||||
@@ -290,6 +290,7 @@ class PythonStubGenerator {
|
|||||||
void GenerateObjectStub(std::stringstream &stub, const StructDef *struct_def,
|
void GenerateObjectStub(std::stringstream &stub, const StructDef *struct_def,
|
||||||
Imports *imports) const {
|
Imports *imports) const {
|
||||||
std::string name = namer_.ObjectType(*struct_def);
|
std::string name = namer_.ObjectType(*struct_def);
|
||||||
|
imports->Export(ModuleFor(struct_def), namer_.Type(*struct_def));
|
||||||
|
|
||||||
stub << "class " << name;
|
stub << "class " << name;
|
||||||
if (version_.major != 3) stub << "(object)";
|
if (version_.major != 3) stub << "(object)";
|
||||||
@@ -327,6 +328,7 @@ class PythonStubGenerator {
|
|||||||
void GenerateStructStub(std::stringstream &stub, const StructDef *struct_def,
|
void GenerateStructStub(std::stringstream &stub, const StructDef *struct_def,
|
||||||
Imports *imports) const {
|
Imports *imports) const {
|
||||||
std::string type = namer_.Type(*struct_def);
|
std::string type = namer_.Type(*struct_def);
|
||||||
|
imports->Export(ModuleFor(struct_def), namer_.Type(*struct_def));
|
||||||
|
|
||||||
stub << "class " << type;
|
stub << "class " << type;
|
||||||
if (version_.major != 3) stub << "(object)";
|
if (version_.major != 3) stub << "(object)";
|
||||||
@@ -545,6 +547,7 @@ class PythonStubGenerator {
|
|||||||
void GenerateEnumStub(std::stringstream &stub, const EnumDef *enum_def,
|
void GenerateEnumStub(std::stringstream &stub, const EnumDef *enum_def,
|
||||||
Imports *imports) const {
|
Imports *imports) const {
|
||||||
stub << "class " << namer_.Type(*enum_def);
|
stub << "class " << namer_.Type(*enum_def);
|
||||||
|
imports->Export(ModuleFor(enum_def), namer_.Type(*enum_def));
|
||||||
|
|
||||||
if (version_.major == 3){
|
if (version_.major == 3){
|
||||||
imports->Import("enum", "IntEnum");
|
imports->Import("enum", "IntEnum");
|
||||||
@@ -589,18 +592,32 @@ class PythonStubGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove imports from exports
|
||||||
|
for (const Import &import : imports.exports) {
|
||||||
|
if (import.name == "") {
|
||||||
|
modules.erase(import.module);
|
||||||
|
} else {
|
||||||
|
auto search = names_by_module.find(import.module);
|
||||||
|
if (search != names_by_module.end()) {
|
||||||
|
search->second.erase(import.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (const std::string &module : modules) {
|
for (const std::string &module : modules) {
|
||||||
ss << "import " << module << '\n';
|
ss << "import " << module << '\n';
|
||||||
}
|
}
|
||||||
for (const auto &import : names_by_module) {
|
for (const auto &import : names_by_module) {
|
||||||
ss << "from " << import.first << " import ";
|
if (!import.second.empty()) {
|
||||||
size_t i = 0;
|
ss << "from " << import.first << " import ";
|
||||||
for (const std::string &name : import.second) {
|
size_t i = 0;
|
||||||
if (i > 0) ss << ", ";
|
for (const std::string &name : import.second) {
|
||||||
ss << name;
|
if (i > 0) ss << ", ";
|
||||||
++i;
|
ss << name;
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
ss << '\n';
|
||||||
}
|
}
|
||||||
ss << '\n';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import numpy as np
|
|||||||
|
|
||||||
import flatbuffers
|
import flatbuffers
|
||||||
import typing
|
import typing
|
||||||
from MyGame.Example.ArrayStruct import ArrayStruct
|
|
||||||
from MyGame.Example.NestedStruct import NestedStruct, NestedStructT
|
from MyGame.Example.NestedStruct import NestedStruct, NestedStructT
|
||||||
from MyGame.Example.TestEnum import TestEnum
|
from MyGame.Example.TestEnum import TestEnum
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import numpy as np
|
|||||||
import flatbuffers
|
import flatbuffers
|
||||||
import typing
|
import typing
|
||||||
from MyGame.Example.ArrayStruct import ArrayStruct, ArrayStructT
|
from MyGame.Example.ArrayStruct import ArrayStruct, ArrayStructT
|
||||||
from MyGame.Example.ArrayTable import ArrayTable
|
|
||||||
|
|
||||||
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
|
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import numpy as np
|
|||||||
|
|
||||||
import flatbuffers
|
import flatbuffers
|
||||||
import typing
|
import typing
|
||||||
from MyGame.Example.NestedStruct import NestedStruct
|
|
||||||
from MyGame.Example.TestEnum import TestEnum
|
from MyGame.Example.TestEnum import TestEnum
|
||||||
|
|
||||||
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
|
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import numpy as np
|
|||||||
|
|
||||||
import flatbuffers
|
import flatbuffers
|
||||||
import typing
|
import typing
|
||||||
from MyGame.Example.NestedUnion.Any import Any
|
|
||||||
from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnum
|
from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnum
|
||||||
from MyGame.Example.NestedUnion.Vec3 import Vec3
|
from MyGame.Example.NestedUnion.Vec3 import Vec3
|
||||||
from flatbuffers import table
|
from flatbuffers import table
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import numpy as np
|
|||||||
import flatbuffers
|
import flatbuffers
|
||||||
import typing
|
import typing
|
||||||
from MyGame.Example.NestedUnion.Any import Any
|
from MyGame.Example.NestedUnion.Any import Any
|
||||||
from MyGame.Example.NestedUnion.NestedUnionTest import NestedUnionTest
|
|
||||||
from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnumT
|
from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnumT
|
||||||
from MyGame.Example.NestedUnion.Vec3 import Vec3T
|
from MyGame.Example.NestedUnion.Vec3 import Vec3T
|
||||||
from flatbuffers import table
|
from flatbuffers import table
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import numpy as np
|
|||||||
|
|
||||||
import flatbuffers
|
import flatbuffers
|
||||||
import typing
|
import typing
|
||||||
from MyGame.Example.NestedUnion.Test import Test
|
|
||||||
|
|
||||||
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
|
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import numpy as np
|
|||||||
import flatbuffers
|
import flatbuffers
|
||||||
import typing
|
import typing
|
||||||
from MyGame.Example.NestedUnion.Color import Color
|
from MyGame.Example.NestedUnion.Color import Color
|
||||||
from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnum
|
|
||||||
|
|
||||||
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
|
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import flatbuffers
|
|||||||
import typing
|
import typing
|
||||||
from MyGame.Example.NestedUnion.Color import Color
|
from MyGame.Example.NestedUnion.Color import Color
|
||||||
from MyGame.Example.NestedUnion.Test import Test, TestT
|
from MyGame.Example.NestedUnion.Test import Test, TestT
|
||||||
from MyGame.Example.NestedUnion.Vec3 import Vec3
|
|
||||||
|
|
||||||
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
|
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import numpy as np
|
|||||||
|
|
||||||
import flatbuffers
|
import flatbuffers
|
||||||
import typing
|
import typing
|
||||||
from MyGame.MonsterExtra import MonsterExtra
|
|
||||||
|
|
||||||
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
|
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user