protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

[python] Add python_package and --prefix argument

Open complynx opened this issue 1 year ago • 6 comments

What language does this apply to? Python generation

Describe the problem you are trying to solve. The problem is that python generated files are generated in global import scope, using package as python package. This requires path mangling, presents a lot of issues when using protos in libraries, and creates potential risks of collision.

Describe the solution you'd like

  1. It would be great to have a python_package option as for java, go and other languages.
  2. It'd be really helpful to have a compiler config, something like --python=package_prefix=my.package.protos.

Describe alternatives you've considered

  1. Modifying the generated sources — one can modify the sources using AST, but that is a bad decision.
  2. Changing the proto package — can not be done to dependencies, as that will break the code (eg protovalidate).

Also, absence of the feature allows developers to create generator plugins with bad generating decisions. For instance, one of the most popular 3d-party plugins, mypy-protobuf, brings the package structure all over the place, which makes modification through AST extremely hard.

complynx avatar Nov 11 '24 11:11 complynx

The issue described here seems related to https://github.com/protocolbuffers/protobuf/issues/1491, which is related to a larger theme of "Python users need a well-lit path for using protobufs in open-source."

What we can recommend (and what more closely corresponds to Google's assumptions), as described in https://github.com/protocolbuffers/protobuf/issues/1491#issuecomment-263772124, is that we generally keep the proto files and the generated files in the same directory. Outside of Google, it's not uncommon to want to keep all the generated code into a separate directory and have code that are able to use relative imports. We can't easily support this due to what Josh described in https://github.com/protocolbuffers/protobuf/issues/1491#issuecomment-263924909.

Having a prefix might be a good solution, but it should be a file option, not a compiler flag. It would be nice if the option is at a directory level so an "organization" can specify a common prefix.

tonyliaoss avatar Dec 13 '24 22:12 tonyliaoss

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Mar 14 '25 10:03 github-actions[bot]

Still active.

niloc132 avatar Mar 14 '25 11:03 niloc132

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Sep 14 '25 10:09 github-actions[bot]

Active. +1

chimaerase avatar Sep 15 '25 16:09 chimaerase

Would also appreciate that feature.

avrilogic avatar Oct 29 '25 14:10 avrilogic