nipunn1313 / mypy-protobuf

open source tools to generate mypy stubs from protobufs
Apache License 2.0
654 stars 80 forks source link

`error: Duplicate argument "self" in function definition` #632

Open rbtz-openai opened 2 months ago

rbtz-openai commented 2 months ago

I'm using mypy-protobuf to generate types for the envoy protos and getting the following error:

envoy/config/core/v3/config_source_pb2.pyi:109: error: Duplicate argument "self" in function definition

This is due to the original protobuf having an oneof field with a self as a value:

 oneof config_source_specifier {
    option (validate.required) = true;
    string path = 1 [deprecated = true, (envoy.annotations.deprecated_at_minor_version) = "3.0"];
    PathConfigSource path_config_source = 8;
    ApiConfigSource api_config_source = 2;
    AggregatedConfigSource ads = 3;
    SelfConfigSource self = 5;
  }

A snippet from the generated pyi:

class ConfigSource(_message.Message):
    __slots__ = ("authorities", "path", "path_config_source", "api_config_source", "ads", "self", "initial_fetch_timeout", "resource_api_version")
    ...
    path_config_source: PathConfigSource
    api_config_source: ApiConfigSource
    ads: AggregatedConfigSource
    self: SelfConfigSource
    ...
    def __init__(self, authorities: _Optional[_Iterable[_Union[_authority_pb2.Authority, _Mapping]]] = ..., path: _Optional[str] = ..., path_config_source: _Optional[_Union[PathConfigSource, _Mapping]] = ..., api_config_source: _Optional[_Union[ApiConfigSource, _Mapping]] = ..., ads: _Optional[_Union[AggregatedConfigSource, _Mapping]] = ..., self: _Optional[_Union[SelfConfigSource, _Mapping]] = ..., initial_fetch_timeout: _Optional[_Union[_duration_pb2.Duration, _Mapping]] = ..., resource_api_version: _Optional[_Union[ApiVersion, str]] = ...) -> None: ...

I'm using the mypy-protobuf from the buf.build sdk:

python3 -m pip install envoyproxy-envoy-community-nipunn1313-mypy==3.6.0.1.20240722201322+b1787804afc0 --extra-index-url https://buf.build/gen/python
rbtz-openai commented 2 months ago

cc: @nipunn1313, long time no see btw! =))

nipunn1313 commented 1 month ago

Good to hear from you! It's probably because it's a reserved keyword. We should be able to work around it. We can take a look at what the underlying constructor chooses to do in the google generated code and match typing to that.