Fix python tests (#7960)

* Don't generate types unless --python-typing specified

Fixes https://github.com/google/flatbuffers/issues/7944

* Fix incorrect import statements

Fixes https://github.com/google/flatbuffers/issues/7951

* Fix $PYTHONPATH in PythonTest.sh

Regressed from https://github.com/google/flatbuffers/pull/7529

* PythonTest: fail if something goes wrong

GitHub Actions runs `bash PythonTest.sh`, and thus failures were not
visible.

* Build flatc for Python tests

* Regenerate codes

---------

Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
Chih-Hsuan Yen
2023-05-18 04:10:51 +08:00
committed by GitHub
parent cb14043f82
commit a352bdbc34
25 changed files with 234 additions and 224 deletions

View File

@@ -275,7 +275,7 @@ class PythonGenerator : public BaseGenerator {
code += namer_.Method(field);
const ImportMapEntry import_entry = {
"." + GenPackageReference(field.value.type), TypeName(field)
GenPackageReference(field.value.type), TypeName(field)
};
if (parser_.opts.python_typing) {
@@ -337,7 +337,7 @@ class PythonGenerator : public BaseGenerator {
code += namer_.Method(field) + "(self)";
const ImportMapEntry import_entry = {
"." + GenPackageReference(field.value.type), TypeName(field)
GenPackageReference(field.value.type), TypeName(field)
};
if (parser_.opts.python_typing) {
@@ -446,7 +446,7 @@ class PythonGenerator : public BaseGenerator {
GenReceiver(struct_def, code_ptr);
code += namer_.Method(field);
const ImportMapEntry import_entry = {
"." + GenPackageReference(field.value.type), TypeName(field)
GenPackageReference(field.value.type), TypeName(field)
};
if (parser_.opts.python_typing) {
@@ -570,7 +570,7 @@ class PythonGenerator : public BaseGenerator {
std::string qualified_name = NestedFlatbufferType(unqualified_name);
if (qualified_name.empty()) { qualified_name = nested->constant; }
const ImportMapEntry import_entry = { "." + qualified_name,
const ImportMapEntry import_entry = { qualified_name,
unqualified_name };
auto &code = *code_ptr;
@@ -773,8 +773,13 @@ class PythonGenerator : public BaseGenerator {
if (!parser_.opts.one_file && !parser_.opts.python_no_type_prefix_suffix) {
// Generate method without struct name.
code += "def Add" + field_method + "(builder: flatbuffers.Builder, " +
field_var + ": " + field_ty + "):\n";
code += "def Add" + field_method;
if (parser_.opts.python_typing) {
code += "(builder: flatbuffers.Builder, " + field_var + ": " + field_ty;
} else {
code += "(builder, " + field_var;
}
code += "):\n";
code += Indent + namer_.Type(struct_def) + "Add" + field_method;
code += "(builder, ";
code += field_var;