I'm using pb_2 and want to consturct instances of a message but keep facing positional argument error. I can construct instances from all other messages but when it comes to the following example, I get stcuk.
I have the following three messages:
message Foo {
int32 num = 1;
repeated KeyValuePair features = 2;
}
message KeyValuePair {
string key = 1;
Value value = 2;
}
message Value {
oneof oneOfValue {
string strValue = 1;
int64 intValue = 2;
}
}
I want to construct an instance of Foo that looks like this:
foo {
num: 123
features: [KeyValuePair {key: 'key1', value: 'value1'},
KeyValuePair {key: 'key2', value: 'value2'}]
}
I tried the following and keep getting positional argument error ('no positional arguments allowed'):
my_foo = Foo(num=123,
features=[KeyValuePair(key='key1', value='value1'),
KeyValuePair(key='key2', value='value2')])
I am not using any positional arguments. Can anyone help?
Thanks!
With the caveat that I am not familiar with protobuf...aren't you instantiating Foo
incorrectly? You've declared that a KeyValuePair
looks like:
message KeyValuePair {
string key = 1;
Value value = 2;
}
So it maps a string value to a Value
instance. That means you would need:
my_foo = Foo(
num=123,
features=[
KeyValuePair(key="key1", value=Value(strValue="value1")),
KeyValuePair(key="key2", value=Value(strValue="value2")),
],
)
On my system, using your example .proto
file (adding syntax = "proto3";
at the top), that runs without errors.