nelfin / pylint-protobuf

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

map fields incorrect report E1136: Value is unsubscriptable (unsubscriptable-object) #32

Closed mabrowning closed 3 years ago

mabrowning commented 3 years ago

I'm on a roll, but our codebase is huge and I really want to use this plugin!

(edited to include E1135)

$ cat map_test.proto
syntax = "proto3";

message MapTest {
    map <string, string> map_test = 1;
}
$ protoc map_test.proto --python_out=.
$ cat test.py
"""
Map Test!
"""

from map_test_pb2 import MapTest

m = MapTest(map_test={"a":"A"})
print("a" in m.map_test)
print(m.map_test["a"])

$ python test.py
True
A

but:

$ pylint test.py
************* Module test
test.py:8:13: E1135: Value 'm.map_test' doesn't support membership test (unsupported-membership-test)
test.py:9:6: E1136: Value 'm.map_test' is unsubscriptable (unsubscriptable-object)

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

I've not really worked with protobuf map fields before so I didn't have any cases covering them. I should probably just disable all of the typechecker messages on the protobuf ClassDefs, but I'll see if there's anything to be saved from them.

nelfin commented 3 years ago

pip install git+https://github.com/nelfin/pylint-protobuf@da4e6453a7dfd8484fdb16e925e25c94d7a467e4

This change adds a __getitem__ method to the generated MapTestEntry type, which satisfies the pylint typecheck checker.

nelfin commented 3 years ago

This has been released in v0.15.0