Consider following structure
message Fly {
uint32 dtime = 1;
}
But HasField
function doesnt work:
>>> d.ListFields()[0][0].name
'dtime'
>>> d.ListFields()[0][0].full_name
'Fly.dtime'
>>>
>>> d.HasField('dtime')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 825, in HasField
raise ValueError(error_msg % field_name)
ValueError: Protocol message has no non-repeated submessage field "dtime"
>>> d.HasField('Fly.dtime')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 825, in HasField
raise ValueError(error_msg % field_name)
ValueError: Protocol message has no non-repeated submessage field "Fly.dtime"
What parameters does HasField
expects?
I think you've misunderstood what HasField()
does. It does not check whether a protobuf type defines a specific field by a name. What it does is to check if, given the name of a message field, that that field is set for the current instance.
As the documentation notes, calling HasField for a non-message field in proto3 will raise an error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With