protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

PYI generator: print values as fields of enum

Open dbeinder opened this issue 3 years ago • 2 comments

When generating PYI for python, enums don't have any fields and your IDE shows errors when referring to a value of an enum using MyEnum.Foo

Example:

syntax = "proto3";
enum MyEnum {
	Abc = 0;
	Foo = 1;
	Bar = 2;
};

$> protoc --pyi_out=. myenum.proto

Current PYI Output:

from google.protobuf.internal import enum_type_wrapper as _enum_type_wrapper
from google.protobuf import descriptor as _descriptor
from typing import ClassVar as _ClassVar

DESCRIPTOR: _descriptor.FileDescriptor

class MyEnum(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
    __slots__ = []
Abc: MyEnum
Foo: MyEnum
Bar: MyEnum

With my changes:

from google.protobuf.internal import enum_type_wrapper as _enum_type_wrapper
from google.protobuf import descriptor as _descriptor
from typing import ClassVar as _ClassVar

DESCRIPTOR: _descriptor.FileDescriptor

class MyEnum(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
    __slots__ = []
    Abc: MyEnum
    Foo: MyEnum
    Bar: MyEnum

Abc: MyEnum
Foo: MyEnum
Bar: MyEnum

It's a bit ugly to have all enum values twice in the interface file, but it is an accurate representation of the generated Python interface - all the values are always in the enum class as well as the parent block. The change also works with enums nested inside of classes.

dbeinder avatar Aug 09 '22 12:08 dbeinder

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

google-cla[bot] avatar Aug 09 '22 12:08 google-cla[bot]

Thanks for the PR We have added some tests for stub, I may need to port this change into our internal first and apply to the tests.

anandolee avatar Sep 13 '22 17:09 anandolee

An internal change is under review. Close this one for clean up

anandolee avatar Oct 04 '22 19:10 anandolee