Message and Field

class proto.message.Message(mapping=None, **kwargs)[source]

The abstract base class for a message.

  • mapping (Union[dict, Message]) – A dictionary or message to be used to determine the values for this message.
  • 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.

  • 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 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
class proto.fields.Field(proto_type, *, number: int, message=None, enum=None, oneof: str = None, json_name: str = None)[source]

A representation of a type of field in protocol buffers.


Return the descriptor for the field.


Return the name of the field.


Return the package of the field.


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)[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.