-
Notifications
You must be signed in to change notification settings - Fork 15.9k
feat(python_pyi): add generated pyi to py_proto_library #19528
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
Conversation
Since protoc supports `pyi_out`, allow py_proto_library to generate the pyi files for editor support (otherwise, most editors cannot autocomplete with proto python output). References: protocolbuffers#1293 Test: There are no tests specifically for py_proto_library, but when I run: `~/protobuf/examples$ bazel build //:addressbook_py_pb2` I get: ``` INFO: Analyzed target //:addressbook_py_pb2 (0 packages loaded, 0 targets configured). INFO: Found 1 target... Target //:addressbook_py_pb2 up-to-date: bazel-bin/external/protobuf~/src/google/protobuf/_virtual_imports/timestamp_proto/google/protobuf/timestamp_pb2.py bazel-bin/external/protobuf~/src/google/protobuf/_virtual_imports/timestamp_proto/google/protobuf/timestamp_pb2.pyi bazel-bin/addressbook_pb2.py bazel-bin/addressbook_pb2.pyi ```
| if plugin_output: | ||
| args.add(plugin_output, format = proto_lang_toolchain_info.out_replacement_format_flag) | ||
| if proto_lang_toolchain_info.out_replacement_format_flag.startswith("--python"): | ||
| args.add(plugin_output, format = "--pyi_out=%s") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not entirely sure the best way to do this. I would prefer to just add the --pyi_out to python's proto_lang_toolchain instantiation but the command_line = "--python_out=%s", can only contain one %s string.
This is my first time in this code base so I would appreciate any suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should have you define another toolchain here for pyi:
protobuf/python/build_targets.bzl
Lines 553 to 561 in 73a5afe
| proto_lang_toolchain( | |
| name = "python_toolchain", | |
| command_line = "--python_out=%s", | |
| progress_message = "Generating Python proto_library %{label}", | |
| runtime = ":protobuf_python", | |
| # NOTE: This isn't *actually* public. It's an implicit dependency of py_proto_library, | |
| # so must be public so user usages of the rule can reference it. | |
| visibility = ["//visibility:public"], | |
| ) |
Then it can be another dependency to the rule, and we can invoke proto_common.compile() twice, once for each toolchain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR should not require changing proto_common.bzl. That is common code used by all proto rules, and shouldn't know anything about .pyi.
|
Why relying on the output of py_proto_library is required? I would think that it should be consumed by other bazel rules. Could you just run protoc to get the desired result? |
|
I think that The resulting pyi files could be used as input to, for example, another rule that generates documentation based on build output. But I also understand where you are coming from. These pyi files are not needed at runtime. |
|
Sorry for the slow review. I've just added some suggestions for how to do this without changing |
|
#21567 seems to do this the way Josh was suggesting, closing this to follow up on that PR |
Since protoc supports
pyi_out, allow py_proto_library to generate the pyi files for editor support (otherwise, most editors cannot autocomplete with proto python output).References: #1293
Testing:
There are no tests specifically for py_proto_library, but when I run:
~/protobuf/examples$ bazel build //:addressbook_py_pb2I get: