9

When I tried to set element of a map<string,Y> dict of X ValueError raised.

"Direct assignment of submessage not allowed"

My experiment code is

syntax = "proto3";

message X {
  map<string,Y> dict = 1;
}

message Y {
  int32 v = 1;
}

And python code

x = x_pb2.X()
y = x_pb2.Y()
x.data['a'] = y

then error raised

Traceback (most recent call last):
  File "x.py", line 8, in <module>
    x.data['a'] = y
ValueError: Direct assignment of submessage not allowed

How can I work around this problem?

1
  • Where is data coming from here?! Commented Jul 19, 2022 at 14:36

1 Answer 1

12

I guess this is optimal usage pattern

x = x_pb2.X()
x.data['a'].v = 1

And another option is using CopyFrom

x = x_pb2.X()
y = x_pb2.Y()
y.v=2
x.data['a'].CopyFrom(y)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.