Message and Field

class proto.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 be used to determine the values for this message.
  • ignore_unknown_fields (Optional(bool)) – If True, do not raise errors for unknown fields. Only applied if mapping is a mapping type or there are keyword parameters.
  • kwargs (dict) – Keys and values corresponding to the fields of the message.
classmethod pb(obj=None, *, coerce: bool = False)

Return the underlying protobuf Message class or instance.

Parameters:
  • obj – If provided, and an instance of cls, return the underlying protobuf instance.
  • coerce (bool) – If provided, will attempt to coerce obj to cls if it is not already an instance.
classmethod wrap(pb)

Return a Message object that shallowly wraps the descriptor.

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

Return the serialized proto.

Parameters:instance – An instance of this message type, or something compatible (accepted by the type’s constructor).
Returns:The serialized representation of the protocol buffer.
Return type:bytes
classmethod deserialize(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 this method was called.
Return type:Message
classmethod to_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 something compatible (accepted by the type’s constructor).
  • use_integers_for_enums (Optional(bool)) – An option that determines whether enum values should be represented by strings (False) or integers (True). Default is True.
  • preserving_proto_field_name (Optional(bool)) – An option that determines whether field name representations preserve proto case (snake_case) or use lowerCamelCase. Default is False.
Returns:

The json string representation of the protocol buffer.

Return type:

str

classmethod from_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 errors for unknown fields.
Returns:

An instance of the message class against which this method was called.

Return type:

Message

classmethod to_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 something compatible (accepted by the type’s constructor).
  • use_integers_for_enums (Optional(bool)) – An option that determines whether enum values should be represented by strings (False) or integers (True). Default is True.
  • preserving_proto_field_name (Optional(bool)) – An option that determines whether field name representations preserve proto case (snake_case) or use lowerCamelCase. Default is True.
  • including_default_value_fields (Optional(bool)) – An option that determines 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

classmethod copy_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.
class proto.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 None for primitives.

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

A representation of a map field in protocol buffers.

class proto.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.

class proto.enums.Enum[source]

A enum object that also builds a protobuf enum descriptor.

class proto.enums.ProtoEnumMeta[source]

A metaclass for building and registering protobuf enums.