danielgtaylor / python-betterproto

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

Generated class name change proto casing #263

Open alk-sramond opened 2 years ago

alk-sramond commented 2 years ago

Given the following protobuf:

message ABCAnswerDeleted {
    uint64 test_id = 1;
}

Version: betterproto[compiler] -> 2.0.0b2 or 2.0.0b3 gives

@dataclass(eq=False, repr=False)
class AbcAnswerDeleted(betterproto.Message):
    test_id: int = betterproto.uint64_field(1)

    def __post_init__(self) -> None:
        super().__post_init__()

Version: betterproto[compiler] -> 1.2.5 gives

@dataclass
class ABCAnswerDeleted(betterproto.Message):
    test_id: int = betterproto.uint64_field(1)

In 2.0.0b2 classname ABC is turned into Abc. Is this something new to betterproto 2 ? Is there an option to specify to allow class-name conservation ?

Thanks for the lib and for the support :)

Seems to be coming from: https://github.com/danielgtaylor/python-betterproto/blob/v2.0.0b3/src/betterproto/compile/importing.py#L60

https://github.com/danielgtaylor/python-betterproto/blob/v2.0.0b3/src/betterproto/plugin/models.py#L302

alk-sramond commented 2 years ago

If that can help the regexp is here: https://github.com/danielgtaylor/python-betterproto/blob/master/src/betterproto/casing.py#L14 we can solve locally by replacing WORD_UPPER -> [A-Z]+(?![a-z])[0-9]* with [A-Z](?![a-z])[0-9]* (not sure yet how it impact snake_case)

We then get ABCDeletedAnswer

GauBen commented 3 months ago

Upping this issue as it makes usage of a protobuf registry hard and inconsistent:

For the following protobuf file

package namespace;

message APIRequest {
  string api_url = 1;
}

The expected message name is 'namespace.APIRequest' but betterproto will create an ApiRequest class instead. As we use cls.__module__ + '.' + cls.__name__ to produce the message name (is there a better way? [1]), things don't map properly across languages.

Thanks in advance


  1. protobuf-es exposes a typeName prop https://github.com/bufbuild/protobuf-es/blob/main/docs/runtime_api.md#message-types