Fix pyi top level messages/enums in public imports

Ported from https://github.com/protocolbuffers/protobuf/pull/13424
According to PEP 484 https://peps.python.org/pep-0484/#stub-files
Modules and variables imported into the stub are not considered exported from the stub unless the import uses the import ... as ... form or the equivalent from ... import ... as ... form.

PiperOrigin-RevId: 552945770
pull/13384/head
Jie Luo 1 year ago committed by Copybara-Service
parent 961779e164
commit b09aa967a6
  1. 10
      src/google/protobuf/compiler/python/pyi_generator.cc

@ -283,14 +283,14 @@ void PyiGenerator::PrintImports() const {
std::string module_name = StrippedModuleName(public_dep->name()); std::string module_name = StrippedModuleName(public_dep->name());
// Top level messages in public imports // Top level messages in public imports
for (int i = 0; i < public_dep->message_type_count(); ++i) { for (int i = 0; i < public_dep->message_type_count(); ++i) {
printer_->Print("from $module$ import $message_class$\n", "module", printer_->Print(
module_name, "message_class", "from $module$ import $message_class$ as $message_class$\n", "module",
public_dep->message_type(i)->name()); module_name, "message_class", public_dep->message_type(i)->name());
} }
// Top level enums for public imports // Top level enums for public imports
for (int i = 0; i < public_dep->enum_type_count(); ++i) { for (int i = 0; i < public_dep->enum_type_count(); ++i) {
printer_->Print("from $module$ import $enum_class$\n", "module", printer_->Print("from $module$ import $enum_class$ as $enum_class$\n",
module_name, "enum_class", "module", module_name, "enum_class",
public_dep->enum_type(i)->name()); public_dep->enum_type(i)->name());
} }
} }

Loading…
Cancel
Save