如何在 Python 中设置一个空消息的 ProtoBuf 字段?

How to set a ProtoBuf field which is an empty message in Python?

以下是Google协议缓冲区(.proto)文件

的内容
message First
{
    required uint32 field1 = 1;

    optional MessageType1 request = 2;
}

message MessageType1
{
}

我想设置 MessageType1 字段请求。但我认为这是一个错误:

AttributeError: Assignment not allowed to composite field "request" in protocol message object.

如何在Python中设置此空消息的值?

在 Proto Buffer 的 Message class 的源代码中得到了这个。

  def SetInParent(self):
    """Mark this as present in the parent.

    This normally happens automatically when you assign a field of a
    sub-message, but sometimes you want to make the sub-message
    present while keeping it empty.  If you find yourself using this,
    you may want to reconsider your design."""

所以设置这样一个空消息的方法是调用这个函数:

first.request.SetInParent()