Skip to content

Issue with nested enums with gRPC #395

Closed
@trickleup

Description

@trickleup

Thanks for a great library. I've found an issue when working with nested enums and gRPC service definitions. It looks somewhat similar to #212, but doesn't seem the same, from what I can see.

better proto version: 2.0.0b4
protoc version: 3.19.4

This proto definition works:

syntax = "proto3";

package hello;

service GreetingAPI {
  rpc SayHello (Greeting)
    returns (Greeting) {}
}

// Greeting represents a message you can tell a user.
message Greeting {
  Foo foo = 1;
}

enum Foo {
  FOO_UNSPECIFIED = 0;
}

But when I nest the enum, like this:

syntax = "proto3";

package hello;

service GreetingAPI {
  rpc SayHello (Greeting)
    returns (Greeting) {}
}

// Greeting represents a message you can tell a user.
message Greeting {
  Foo foo = 1;
  enum Foo {
    FOO_UNSPECIFIED = 0;
  }
}

It fails with the following error:

Traceback (most recent call last):
  File "/Users/johan.genberg/projects/testing/betterproto/.venv/bin/protoc-gen-python_betterproto", line 8, in <module>
    sys.exit(main())
  File "/Users/johan.genberg/projects/testing/betterproto/.venv/lib/python3.9/site-packages/betterproto/plugin/main.py", line 32, in main
    response = generate_code(request)
  File "/Users/johan.genberg/projects/testing/betterproto/.venv/lib/python3.9/site-packages/betterproto/plugin/parser.py", line 107, in generate_code
    read_protobuf_service(service, index, output_package)
  File "/Users/johan.genberg/projects/testing/betterproto/.venv/lib/python3.9/site-packages/betterproto/plugin/parser.py", line 191, in read_protobuf_service
    ServiceMethodCompiler(
  File "<string>", line 7, in __init__
  File "/Users/johan.genberg/projects/testing/betterproto/.venv/lib/python3.9/site-packages/betterproto/plugin/models.py", line 683, in __post_init__
    self.mutable_default_args  # ensure this is called before rendering
  File "/Users/johan.genberg/projects/testing/betterproto/.venv/lib/python3.9/site-packages/betterproto/plugin/models.py", line 720, in mutable_default_args
    and f.default_value_string != "None"
  File "/Users/johan.genberg/projects/testing/betterproto/.venv/lib/python3.9/site-packages/betterproto/plugin/models.py", line 486, in default_value_string
    enum = next(
StopIteration
--python_betterproto_out: protoc-gen-python_betterproto: Plugin failed with status code 1.

I run betterproto like this:

protoc -I . --python_betterproto_out=lib test_fail.proto

The error seems to be due to some naming collision. Also, it only appears if the service definition is in the file. If I remove it, the nested enum works well.

I can work around this by not nesting the enum. But still wanted to report it, see if there was a more long term fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions