Closed pcpLiu closed 11 months ago
looks like we can work around with custom plugin
How do we generate pyi files using custom plugin?
I'm also interested in this! @pcpLiu Can you describe how you solved it?
I'd urge reopening this, since this comes in handy for type-checking and autocomplete dev experience. The examples in grpc feature the pyi files as if they're the default: https://github.com/grpc/grpc/blob/master/examples/python/async_streaming/phone_pb2.pyi
Update: Alternatively, I create a new issue as a feature request to support pyi output, since this was posed as a question.
Hey guys, so in the core we created a plugin like
load("@rules_proto_grpc//:defs.bzl", "proto_plugin")
proto_plugin(
name = "pyi_plugin",
exclusions = [
"google/protobuf",
],
outputs = ["{protopath|python}_pb2.pyi"],
protoc_plugin_name = "pyi",
visibility = ["//visibility:public"],
)
This plugin just simply includes pyi
files generated from grpc compiling step into the output dir. At least for our version, grpc compiling by default generates pyi files.
Then, depending on your needs, recreate the rule by copying this and injecting the new plugin defined above, you gonna get pyi files in your bazel output
Our impl is pretty hacky and embedded into our internal repo, so we don't have a neat code snippets for sharing right now. You should be able to make it work follow the similar thoughts.
Thanks for sharing!
Hm after reading the source code at https://github.com/protocolbuffers/protobuf/blob/a4f9ddd8fc2ff1afa6697059d494a9bf0c09c680/src/google/protobuf/compiler/python/generator.cc#L197-L200 , I realized you can pass options
= pyi_out
to the python plugin.
So the current rules already support pyi output. There's just no example of this type of usage, perhaps I can submit a PR on to add an example.
@chrisirhc - an example would be helpful, I am looking for a way to add pyi_out
as well.
Description
Hi,
I wanna generate
pyi
so i need to setup--pyi_out
dir in extra_protoc_args. I want it to be generated the same dir with generated pythons files.However, I'm not sure how to properly specify this dir in the context of bazel such that I don't need to hardcode it.
Any suggestions? Thanks