Skip to content

Support .pyi generation in py_proto_library #32564

@ferstlf

Description

@ferstlf

Hi!

Is your feature request related to a problem? Please describe.

The general problem: When working with protos/gRPC and Bazel in an IDE i'd like to have proper language support, i.e. import resolution, auto-completion etc. should work for automatically generated proto classes.

With Bazel, import resolution is a bit tricky, but essentially I can configure in my IDE/language server the relevant Python import paths inside Bazels temporary folders (e.g. bazel-bin or bazel-bin/external/com_github_grpc_grpc). However, the .py files generated by protoc use Metaclasses and language servers such as Pylance in VS Code do not understand these to offer proper auto-completion and more.

Since Protobuf v3.20.0, protoc is natively able to generate python stubs (.pyi) with the --pyi_out flag. If one generates a *_pb2.pyi file for every *_pb2.py file this should be understood by most language servers automatically and things like auto-completion and static type-checking with proto classes should just work (I can at least confirm for Pylance/VS Code).

Describe the solution you'd like

We use gRPC's py_proto_libary rule in our Bazel workspace and also expect our users to use it in their workspaces (which depend on our workspace). It would be great if the py_proto_libary rule would generate .pyi files in addition to .py files - either by default (since it is not very costly) or enabled via a boolean flag on the py_proto_libary rule.

It should be as simple as adding the --pyi_out flag to the protoc params here and then making sure that py_proto_libary correctly includes or excludes the additionally generated .pyi files from the output files, runfiles and PyInfo which it declares as its output.

Describe alternatives you've considered

Additional context

Let me know if this would make sense to you. I already have this working as a patch to our Bazel import of the gRPC workspace. I could try to turn this into a PR myself.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions