Protobuf: Question: How to set oneof fields in python?

Created on 7 Aug 2018  路  5Comments  路  Source: protocolbuffers/protobuf

message Test {
oneof test_oneof {
MessageA a = 1;
MessageB b = 2;
}
message MessageA {鈥
message MessageB {鈥
}
test =Test()
test.a = Test.MessageA()
it always complains:Assignment not allowed to field "a" in protocol message object.

python question

Most helpful comment

"Assignment not allowed to field "a" in protocol message object." is not a oneof problem, it is complain about assignment for message field.

There are two ways you can do:

test.a.CopyFrom(...)
or
test.a.sub_field = ...

All 5 comments

"Assignment not allowed to field "a" in protocol message object." is not a oneof problem, it is complain about assignment for message field.

There are two ways you can do:

test.a.CopyFrom(...)
or
test.a.sub_field = ...

HI @anandolee , I have the same problem. I have tried test.a.CopyFrom(...), it works, but how to use test.a.sub_field = ?

I want to ask some questions too.

message Test {
     oneof test_oneof {
     MessageA a = 1;
     MessageB b = 2;
     string c = 3;
}
message MessageA {鈥
message MessageB {鈥

test =Test()
test.c = "abcd"
test.a = ...
I want to know why test.a = ... cannot work? The official tutorial explain how to use oneof only with string and int32 types. I have searched on google, and it also lack of answers about how to fill the message fields in oneof.
https://developers.google.com/protocol-buffers/docs/reference/python-generated#oneof

For "sub_field", it means a sub scalar field of MessageA. For example:
https://github.com/google/protobuf/blob/master/python/google/protobuf/internal/message_test.py#L795

Protobuf python does not support assignment for message field, even it is a normal message field instead of oneof, "test.a =" is still not allowed. "You cannot assign a value to an embedded message field. Instead, assigning a value to any field within the child message implies setting the message field in the parent. " :
https://developers.google.com/protocol-buffers/docs/reference/python-generated#embedded_message

Thanks for your reply @anandolee I want to ask the other question, I tried to read the unittest code, but I don't know where does the message_module parameter come from? I think it is the unittest_pb2 or
unittest_proto3_arena_pb2. In my opinion, it is generated by some .proto file, where is the .proto file?
What's the meaning of the golden_message?
@_parameterized.named_parameters(
('_proto2', unittest_pb2),
('_proto3', unittest_proto3_arena_pb2))

Yes, it is unittest_pb2 or unittest_proto3_arena_pb2 (we tested both):
https://github.com/google/protobuf/blob/master/src/google/protobuf/unittest.proto#L64
https://github.com/google/protobuf/blob/master/src/google/protobuf/unittest_proto3_arena.proto#L41

"Golden data" is the wire format for a message and we put the wire format to a "golden data file". This is because we can write wire format in the test directly.

Was this page helpful?
0 / 5 - 0 ratings