protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.24k stars 15.45k forks source link

mypy gave error on accessing Enum value #17655

Open sfc-gh-smao opened 1 month ago

sfc-gh-smao commented 1 month ago

What version of protobuf and what language are you using? Protobuf 4.24.0

Language: Python

What operating system (Linux, Windows, ...) and version? macOS 14.5

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

What did you do? I created the following proto

syntax = "proto3";

enum SomeEnum {
    VALUE_A = 0;
    VALUE_B = 5;
    VALUE_C = 1234;
}

What did you expect to see

According to https://protobuf.dev/reference/python/python-generated/#enum, I expect I can access the enum value via any of the following ways:

value_a = myproto_pb2.SomeEnum.VALUE_A
# or
myproto_pb2.VALUE_A
# or
myproto_pb2.SomeEnum.Value('VALUE_A')

What did you see instead?

When accessing myproto_pb2.SomeEnum.VALUE_A, I got the following error from mypy (version 1.11.0):

error: "type[SomeEnum]" has no attribute "VALUE_A"  [attr-defined]

Is this a known issue? I'm not sure if this is an issue with protobuf, protoc, or mypy.

anandolee commented 1 month ago

I guess the doc is wrong, should be:

value_a = myproto_pb2.SomeEnum.values_by_name['VALUE_A'] or myproto_pb2.VALUE_A or myproto_pb2.SomeEnum.Value('VALUE_A')

sfc-gh-smao commented 1 month ago

value_a = myproto_pb2.SomeEnum.VALUE_A actually works. It's just that mypy gave an error on it.