nelfin / pylint-protobuf

A plugin for making Pylint aware of the fields of protobuf-generated classes
MIT License
29 stars 12 forks source link

enum `.Name()` improperly flagged as error #31

Closed mabrowning closed 3 years ago

mabrowning commented 3 years ago

enum protobuf messages are implemented as EnumTypeWrapper python class, which adds several special methods:

https://github.com/protocolbuffers/protobuf/blob/master/python/google/protobuf/internal/enum_type_wrapper.py#L43-L106

Name, Value, keys, and values. However pylint-protobuf 0.14.1 doesn't seem to recognize these.

Consider:

$ cat direction.proto
syntax = "proto3";

enum Direction {
    UP = 0;
    DOWN = 1;
}
$ protoc direction.proto --python_out=.
$ cat test.py
"""
Enum Test!
"""
from direction_pb2 import Direction
print(Direction.Name(Direction.UP))

$ python test.py
UP

but:

$ pylint test.py
************* Module test
test.py:7:6: E5901: Field 'Name' does not appear in the declared fields of protobuf-generated class 'Direction' and will raise AttributeError on access (protobuf-undefined-attribute)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)
mabrowning commented 3 years ago

Editing https://github.com/nelfin/pylint-protobuf/blob/ce5cb26f1b3cef16c1bb67eaf14bdd5a0082ad5b/pylint_protobuf/__init__.py#L46-L48 to add Name, keys, and values seems to silence the errors I see.