danielgtaylor / python-betterproto

Clean, modern, Python 3.6+ code generator & library for Protobuf 3 and async gRPC
MIT License
1.56k stars 218 forks source link

Import not generated for package/module if only referenced in service declaration. #426

Open adriangaro opened 2 years ago

adriangaro commented 2 years ago

As a basic example I have the below, the generated service will not import the test package, however if i reference any message in the return it imports it correctly.


package test;

import "test2.proto";

message TestMessage {}

service Test {
  rpc test(test2.DemoRequest) returns (TestMessage);
}

From a shallow look seems to stem from the ServiceMethodCompiler.__post_init__ method. The return type is referenced using py_output_message_type which calls get_type_reference which adds it to imports, however nowhere in the __post_init__ is the input type referenced for it to be added with get_type_reference to imports.

adriangaro commented 2 years ago

Adding the line self.py_input_message_type at line 692 in that file seems to work, also betterproto version 2.0.0-beta5

ABaumher commented 1 year ago

For those with this issue who note that the line number is out of date on the current build, here's where that was referring to: https://github.com/danielgtaylor/python-betterproto/blob/8fbf4476a8f4a9c9a8bb07a64cc0bbb379e0afa8/src/betterproto/plugin/models.py#L688-L694