Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum values not defined in enum class #6028

Closed
plannigan opened this issue Apr 12, 2019 · 4 comments
Closed

Enum values not defined in enum class #6028

plannigan opened this issue Apr 12, 2019 · 4 comments

Comments

@plannigan
Copy link

The documentation for enum says:

For example, you can access the values in the three following ways for the following enum in a proto:

enum SomeEnum {
    VALUE_A = 0;
    VALUE_B = 5;
    VALUE_C = 1234;
}
value-a = myproto_pb2.SomeEnum.VALUE_A
# or
myproto_pb2.VALUE_A
# or
myproto_pb2.SomeEnum.Value('VALUE_A')

But value-a = myproto_pb2.SomeEnum.VALUE_A fails.

What version of protobuf and what language are you using?
Version: v3.7.1/v3.6.1
Language: Python

What operating system (Linux, Windows, ...) and version?
Xubuntu 18.04

What runtime / compiler are you using (e.g., python version or gcc version)
gcc

What did you do?

  1. Save the proto definition from the documentation to a file name myproto.proto.
  2. Generate the python code:
./protoc --python_out=. --proto_path=. myproto.proto 
  1. Attempt to access the value in the ways the documentation demonstrates.
python -c "import myproto_pb2; print(myproto_pb2.SomeEnum.Value('VALUE_A')); print(myproto_pb2.VALUE_A); print(myproto_pb2.SomeEnum.VALUE_A);"

What did you expect to see

0
0
0

What did you see instead?

0
0
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'EnumTypeWrapper' object has no attribute 'VALUE_A'
@anandolee
Copy link
Contributor

anandolee commented May 2, 2019

Oh, we updated the document earlier than it should be. The getattr is added in this PR which is not release in v3.7.1:
0de6577#diff-abebaaaa517a7cbc213cb3d39c6e444b

@pkalebu
Copy link

pkalebu commented Oct 17, 2019

what's the correct way to get the value of an enum then?

@markisus
Copy link

@pkalebu It looks like you have to do myproto_pb2.SomeEnum.Value('VALUE_A')

@timgrossmann
Copy link

timgrossmann commented Oct 31, 2019

@markisus Thanks for this, looks like the documentation needs quite some updates to conform to Proto3 😅

Would've needed to read the actual commit changes to get the answer to read the enum values.
Unfortunately due to this changes, even the most downloaded VS-Code plugin will show errors for valid code...

Do they have their documentation source open?
I think I'd be able to update some of it since I'm writing a blog post about it anyway 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants