Message and Field

classproto.message.Message(mapping=None,*,ignore_unknown_fields=False,**kwargs)[source]

The abstract base class for a message.

Parameters:
  • mapping (Union[dict,Message]) – A dictionary or message to beused to determine the values for this message.
  • ignore_unknown_fields (Optional(bool)) – If True, do not raise errors forunknown fields. Only applied ifmapping is a mapping type or thereare keyword parameters.
  • kwargs (dict) – Keys and values corresponding to the fields of themessage.
classmethodpb(obj=None,*,coerce: bool = False)

Return the underlying protobuf Message class or instance.

Parameters:
  • obj – If provided, and an instance ofcls, return theunderlying protobuf instance.
  • coerce (bool) – If provided, will attempt to coerceobj tocls if it is not already an instance.
classmethodwrap(pb)

Return a Message object that shallowly wraps the descriptor.

Parameters:pb – A protocol buffer object, such as would be returned bypb().
classmethodserialize(instance) → bytes

Return the serialized proto.

Parameters:instance – An instance of this message type, or somethingcompatible (accepted by the type’s constructor).
Returns:The serialized representation of the protocol buffer.
Return type:bytes
classmethoddeserialize(payload: bytes) → proto.message.Message

Given a serialized proto, deserialize it into a Message instance.

Parameters:payload (bytes) – The serialized proto.
Returns:An instance of the message class against which thismethod was called.
Return type:Message
classmethodto_json(instance,*,use_integers_for_enums=True,including_default_value_fields=True,preserving_proto_field_name=False) → str

Given a message instance, serialize it to json

Parameters:
  • instance – An instance of this message type, or somethingcompatible (accepted by the type’s constructor).
  • use_integers_for_enums (Optional(bool)) – An option that determines whether enumvalues should be represented by strings (False) or integers (True).Default is True.
  • preserving_proto_field_name (Optional(bool)) – An option thatdetermines whether field name representations preserveproto case (snake_case) or use lowerCamelCase. Default is False.
Returns:

The json string representation of the protocol buffer.

Return type:

str

classmethodfrom_json(payload,*,ignore_unknown_fields=False) → proto.message.Message

Given a json string representing an instance,parse it into a message.

Parameters:
  • paylod – A json string representing a message.
  • ignore_unknown_fields (Optional(bool)) – If True, do not raise errorsfor unknown fields.
Returns:

An instance of the message class against which thismethod was called.

Return type:

Message

classmethodto_dict(instance,*,use_integers_for_enums=True,preserving_proto_field_name=True,including_default_value_fields=True) → proto.message.Message

Given a message instance, return its representation as a python dict.

Parameters:
  • instance – An instance of this message type, or somethingcompatible (accepted by the type’s constructor).
  • use_integers_for_enums (Optional(bool)) – An option that determines whether enumvalues should be represented by strings (False) or integers (True).Default is True.
  • preserving_proto_field_name (Optional(bool)) – An option thatdetermines whether field name representations preserveproto case (snake_case) or use lowerCamelCase. Default is True.
  • including_default_value_fields (Optional(bool)) – An option thatdetermines whether the default field values should be included in the results.Default is True.
Returns:

A representation of the protocol buffer using pythonic data structures.

Messages and map fields are represented as dicts,repeated fields are represented as lists.

Return type:

dict

classmethodcopy_from(instance,other)

Equivalent for protobuf.Message.CopyFrom

Parameters:
  • instance – An instance of this message type
  • other – (Union[dict, ~.Message):A dictionary or message to reinitialize the values for this message.
classproto.fields.Field(proto_type,*,number: int,message=None,enum=None,oneof: str = None,json_name: str = None,optional: bool = False)[source]

A representation of a type of field in protocol buffers.

descriptor

Return the descriptor for the field.

name

Return the name of the field.

package

Return the package of the field.

pb_type

Return the composite type of the field, or the primitive type if a primitive.

classproto.fields.MapField(key_type,value_type,*,number: int,message=None,enum=None)[source]

A representation of a map field in protocol buffers.

classproto.fields.RepeatedField(proto_type,*,number: int,message=None,enum=None,oneof: str = None,json_name: str = None,optional: bool = False)[source]

A representation of a repeated field in protocol buffers.

classproto.enums.Enum[source]

A enum object that also builds a protobuf enum descriptor.

classproto.enums.ProtoEnumMeta[source]

A metaclass for building and registering protobuf enums.