Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for module import prefix on Python compiler #17286

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

chrisirhc
Copy link

@chrisirhc chrisirhc commented Jun 30, 2024

Problem

When generating code in monorepos, some generated code is used with a prefix path, see https://rules-proto-grpc.com/en/latest/lang/python.html .
However, when a prefix_path is used, the generated proto import paths are no longer valid unless the module imports are also prefixed similarly.
This adds an option to support adding an import prefix to the printed imports.

Approach

This follows a similar approach already in use in the objectivec compiler.

How did I validate the approach?

I ran some basic manual tests like the following and validated that the module prefix was added to imports:

bazel run //src/google/protobuf/compiler:protoc -- --python_out=module_
import_prefix=test.prefix,pyi_out:$(PWD) <path to local repo clone>/csharp/compatibility_tests/v3.0.0/protos/src/google/protobuf/unittest_import_p
roto3.proto --proto_path=<path to local repo clone>/csharp/compatibility_tests/v3.0.0/protos/src

The diff when comparing with and without the module_import_prefix=test.prefix parameter:

diff --color=auto -r a/google/protobuf/unittest_import_proto3_pb2.py b/google/protobuf/unittest_import_proto3_pb2.py
25c25
< from google.protobuf import unittest_import_public_proto3_pb2 as google_dot_protobuf_dot_unittest__import__public__proto3__pb2
---
> from test.prefix.google.protobuf import unittest_import_public_proto3_pb2 as google_dot_protobuf_dot_unittest__import__public__proto3__pb2
27c27
< from google.protobuf.unittest_import_public_proto3_pb2 import *
---
> from test.prefix.google.protobuf.unittest_import_public_proto3_pb2 import *
Only in b/google/protobuf: unittest_import_proto3_pb2.pyi

Remaining TODOs

  • Add a test for this feature
  • Make sure this feature works with pyi generation

I couldn't find tests for parameter behavior. plugin_unittest, https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/compiler/python/plugin_unittest.cc doesn't test much of the plugin logic.
I would appreciate some guidance in how to add a test for this feature (or if a test is needed).

Related Issues

@chrisirhc chrisirhc requested a review from a team as a code owner June 30, 2024 02:10
@chrisirhc chrisirhc requested review from anandolee and removed request for a team June 30, 2024 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant