google.protobuf.descriptor

Descriptors essentially contain exactly the information found in a .proto file, in types that make this information accessible in Python.

class google.protobuf.descriptor.Descriptor(name, full_name, filename, containing_type, fields, nested_types, enum_types, extensions, options=None, serialized_options=None, is_extendable=True, extension_ranges=None, oneofs=None, file=None, serialized_start=None, serialized_end=None, syntax=None)

Descriptor for a protocol message type.

A Descriptor instance has the following attributes:

name: (str) Name of this protocol message type. full_name: (str) Fully-qualified name of this protocol message type,

which will include protocol “package” name and the name of any enclosing types.

containing_type: (Descriptor) Reference to the descriptor of the

type containing us, or None if this is top-level.

fields: (list of FieldDescriptors) Field descriptors for all

fields in this type.

fields_by_number: (dict int -> FieldDescriptor) Same FieldDescriptor

objects as in |fields|, but indexed by “number” attribute in each FieldDescriptor.

fields_by_name: (dict str -> FieldDescriptor) Same FieldDescriptor

objects as in |fields|, but indexed by “name” attribute in each FieldDescriptor.

fields_by_camelcase_name: (dict str -> FieldDescriptor) Same

FieldDescriptor objects as in |fields|, but indexed by “camelcase_name” attribute in each FieldDescriptor.

nested_types: (list of Descriptors) Descriptor references

for all protocol message types nested within this one.

nested_types_by_name: (dict str -> Descriptor) Same Descriptor

objects as in |nested_types|, but indexed by “name” attribute in each Descriptor.

enum_types: (list of EnumDescriptors) EnumDescriptor references

for all enums contained within this type.

enum_types_by_name: (dict str ->EnumDescriptor) Same EnumDescriptor

objects as in |enum_types|, but indexed by “name” attribute in each EnumDescriptor.

enum_values_by_name: (dict str -> EnumValueDescriptor) Dict mapping

from enum value name to EnumValueDescriptor for that value.

extensions: (list of FieldDescriptor) All extensions defined directly

within this message type (NOT within a nested type).

extensions_by_name: (dict, string -> FieldDescriptor) Same FieldDescriptor

objects as |extensions|, but indexed by “name” attribute of each FieldDescriptor.

is_extendable: Does this type define any extension ranges?

oneofs: (list of OneofDescriptor) The list of descriptors for oneof fields

in this message.

oneofs_by_name: (dict str -> OneofDescriptor) Same objects as in |oneofs|,

but indexed by “name” attribute.

file: (FileDescriptor) Reference to file descriptor.

CopyToProto(proto)

Copies this to a descriptor_pb2.DescriptorProto.

Parameters

proto – An empty descriptor_pb2.DescriptorProto.

EnumValueName(enum, value)

Returns the string name of an enum value.

This is just a small helper method to simplify a common operation.

Parameters
  • enum – string name of the Enum.

  • value – int, value of the enum.

Returns

string name of the enum value.

Raises

KeyError if either the Enum doesn't exist or the value is not a valid – value for the enum.

GetOptions()

Retrieves descriptor options.

This method returns the options set or creates the default options for the descriptor.

property fields_by_camelcase_name
class google.protobuf.descriptor.DescriptorBase(options, serialized_options, options_class_name)

Descriptors base class.

This class is the base of all descriptor classes. It provides common options related functionality.

has_options

True if the descriptor has non-default options. Usually it is not necessary to read this – just call GetOptions() which will happily return the default instance. However, it’s sometimes useful for efficiency, and also useful inside the protobuf implementation to avoid some bootstrapping issues.

GetOptions()

Retrieves descriptor options.

This method returns the options set or creates the default options for the descriptor.

class google.protobuf.descriptor.EnumDescriptor(name, full_name, filename, values, containing_type=None, options=None, serialized_options=None, file=None, serialized_start=None, serialized_end=None)

Descriptor for an enum defined in a .proto file.

An EnumDescriptor instance has the following attributes:

name: (str) Name of the enum type. full_name: (str) Full name of the type, including package name

and any enclosing type(s).

values: (list of EnumValueDescriptors) List of the values

in this enum.

values_by_name: (dict str -> EnumValueDescriptor) Same as |values|,

but indexed by the “name” field of each EnumValueDescriptor.

values_by_number: (dict int -> EnumValueDescriptor) Same as |values|,

but indexed by the “number” field of each EnumValueDescriptor.

containing_type: (Descriptor) Descriptor of the immediate containing

type of this enum, or None if this is an enum defined at the top level in a .proto file. Set by Descriptor’s constructor if we’re passed into one.

file: (FileDescriptor) Reference to file descriptor. options: (descriptor_pb2.EnumOptions) Enum options message or

None to use default enum options.

CopyToProto(proto)

Copies this to a descriptor_pb2.EnumDescriptorProto.

Parameters

proto – An empty descriptor_pb2.EnumDescriptorProto.

GetOptions()

Retrieves descriptor options.

This method returns the options set or creates the default options for the descriptor.

class google.protobuf.descriptor.EnumValueDescriptor(name, index, number, type=None, options=None, serialized_options=None)

Descriptor for a single value within an enum.

name: (str) Name of this value. index: (int) Dense, 0-indexed index giving the order that this

value appears textually within its enum in the .proto file.

number: (int) Actual number assigned to this enum value. type: (EnumDescriptor) EnumDescriptor to which this value

belongs. Set by EnumDescriptor’s constructor if we’re passed into one.

options: (descriptor_pb2.EnumValueOptions) Enum value options message or

None to use default enum value options options.

GetOptions()

Retrieves descriptor options.

This method returns the options set or creates the default options for the descriptor.

exception google.protobuf.descriptor.Error

Base error for this module.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class google.protobuf.descriptor.FieldDescriptor(name, full_name, index, number, type, cpp_type, label, default_value, message_type, enum_type, containing_type, is_extension, extension_scope, options=None, serialized_options=None, has_default_value=True, containing_oneof=None, json_name=None, file=None)

Descriptor for a single field in a .proto file.

A FieldDescriptor instance has the following attributes:

name: (str) Name of this field, exactly as it appears in .proto. full_name: (str) Name of this field, including containing scope. This is

particularly relevant for extensions.

camelcase_name: (str) Camelcase name of this field. index: (int) Dense, 0-indexed index giving the order that this

field textually appears within its message in the .proto file.

number: (int) Tag number declared for this field in the .proto file.

type: (One of the TYPE_* constants below) Declared type. cpp_type: (One of the CPPTYPE_* constants below) C++ type used to

represent this field.

label: (One of the LABEL_* constants below) Tells whether this

field is optional, required, or repeated.

has_default_value: (bool) True if this field has a default value defined,

otherwise false.

default_value: (Varies) Default value of this field. Only

meaningful for non-repeated scalar fields. Repeated fields should always set this to [], and non-repeated composite fields should always set this to None.

containing_type: (Descriptor) Descriptor of the protocol message

type that contains this field. Set by the Descriptor constructor if we’re passed into one. Somewhat confusingly, for extension fields, this is the descriptor of the EXTENDED message, not the descriptor of the message containing this field. (See is_extension and extension_scope below).

message_type: (Descriptor) If a composite field, a descriptor

of the message type contained in this field. Otherwise, this is None.

enum_type: (EnumDescriptor) If this field contains an enum, a

descriptor of that enum. Otherwise, this is None.

is_extension: True iff this describes an extension field. extension_scope: (Descriptor) Only meaningful if is_extension is True.

Gives the message that immediately contains this extension field. Will be None iff we’re a top-level (file-level) extension field.

options: (descriptor_pb2.FieldOptions) Protocol message field options or

None to use default field options.

containing_oneof: (OneofDescriptor) If the field is a member of a oneof

union, contains its descriptor. Otherwise, None.

file: (FileDescriptor) Reference to file descriptor.

CPPTYPE_BOOL = 7
CPPTYPE_DOUBLE = 5
CPPTYPE_ENUM = 8
CPPTYPE_FLOAT = 6
CPPTYPE_INT32 = 1
CPPTYPE_INT64 = 2
CPPTYPE_MESSAGE = 10
CPPTYPE_STRING = 9
CPPTYPE_UINT32 = 3
CPPTYPE_UINT64 = 4
FIRST_RESERVED_FIELD_NUMBER = 19000
GetOptions()

Retrieves descriptor options.

This method returns the options set or creates the default options for the descriptor.

LABEL_OPTIONAL = 1
LABEL_REPEATED = 3
LABEL_REQUIRED = 2
LAST_RESERVED_FIELD_NUMBER = 19999
MAX_CPPTYPE = 10
MAX_FIELD_NUMBER = 536870911
MAX_LABEL = 3
MAX_TYPE = 18
static ProtoTypeToCppProtoType(proto_type)

Converts from a Python proto type to a C++ Proto Type.

The Python ProtocolBuffer classes specify both the ‘Python’ datatype and the ‘C++’ datatype - and they’re not the same. This helper method should translate from one to another.

Parameters

proto_type – the Python proto type (descriptor.FieldDescriptor.TYPE_*)

Returns

descriptor.FieldDescriptor.CPPTYPE_*, the C++ type.

Raises

TypeTransformationError – when the Python proto type isn’t known.

TYPE_BOOL = 8
TYPE_BYTES = 12
TYPE_DOUBLE = 1
TYPE_ENUM = 14
TYPE_FIXED32 = 7
TYPE_FIXED64 = 6
TYPE_FLOAT = 2
TYPE_GROUP = 10
TYPE_INT32 = 5
TYPE_INT64 = 3
TYPE_MESSAGE = 11
TYPE_SFIXED32 = 15
TYPE_SFIXED64 = 16
TYPE_SINT32 = 17
TYPE_SINT64 = 18
TYPE_STRING = 9
TYPE_UINT32 = 13
TYPE_UINT64 = 4
property camelcase_name
class google.protobuf.descriptor.FileDescriptor(name, package, options=None, serialized_options=None, serialized_pb=None, dependencies=None, public_dependencies=None, syntax=None, pool=None)

Descriptor for a file. Mimics the descriptor_pb2.FileDescriptorProto.

Note that enum_types_by_name, extensions_by_name, and dependencies fields are only set by the message_factory module, and not by the generated proto code.

name: name of file, relative to root of source tree. package: name of the package syntax: string indicating syntax of the file (can be “proto2” or “proto3”) serialized_pb: (str) Byte string of serialized

descriptor_pb2.FileDescriptorProto.

dependencies: List of other FileDescriptors this FileDescriptor depends on. public_dependencies: A list of FileDescriptors, subset of the dependencies

above, which were declared as “public”.

message_types_by_name: Dict of message names and their descriptors. enum_types_by_name: Dict of enum names and their descriptors. extensions_by_name: Dict of extension names and their descriptors. services_by_name: Dict of services names and their descriptors. pool: the DescriptorPool this descriptor belongs to. When not passed to the

constructor, the global default pool is used.

CopyToProto(proto)

Copies this to a descriptor_pb2.FileDescriptorProto.

Parameters

proto – An empty descriptor_pb2.FileDescriptorProto.

GetOptions()

Retrieves descriptor options.

This method returns the options set or creates the default options for the descriptor.

google.protobuf.descriptor.MakeDescriptor(desc_proto, package='', build_file_if_cpp=True, syntax=None)

Make a protobuf Descriptor given a DescriptorProto protobuf.

Handles nested descriptors. Note that this is limited to the scope of defining a message inside of another message. Composite fields can currently only be resolved if the message is defined in the same scope as the field.

Parameters
  • desc_proto – The descriptor_pb2.DescriptorProto protobuf message.

  • package – Optional package name for the new message Descriptor (string).

  • build_file_if_cpp – Update the C++ descriptor pool if api matches. Set to False on recursion, so no duplicates are created.

  • syntax – The syntax/semantics that should be used. Set to “proto3” to get proto3 field presence semantics.

Returns

A Descriptor for protobuf messages.

class google.protobuf.descriptor.MethodDescriptor(name, full_name, index, containing_service, input_type, output_type, options=None, serialized_options=None)

Descriptor for a method in a service.

name: (str) Name of the method within the service. full_name: (str) Full name of method. index: (int) 0-indexed index of the method inside the service. containing_service: (ServiceDescriptor) The service that contains this

method.

input_type: The descriptor of the message that this method accepts. output_type: The descriptor of the message that this method returns. options: (descriptor_pb2.MethodOptions) Method options message or

None to use default method options.

GetOptions()

Retrieves descriptor options.

This method returns the options set or creates the default options for the descriptor.

class google.protobuf.descriptor.OneofDescriptor(name, full_name, index, containing_type, fields, options=None, serialized_options=None)

Descriptor for a oneof field.

name: (str) Name of the oneof field. full_name: (str) Full name of the oneof field, including package name. index: (int) 0-based index giving the order of the oneof field inside

its containing type.

containing_type: (Descriptor) Descriptor of the protocol message

type that contains this field. Set by the Descriptor constructor if we’re passed into one.

fields: (list of FieldDescriptor) The list of field descriptors this

oneof can contain.

GetOptions()

Retrieves descriptor options.

This method returns the options set or creates the default options for the descriptor.

class google.protobuf.descriptor.ServiceDescriptor(name, full_name, index, methods, options=None, serialized_options=None, file=None, serialized_start=None, serialized_end=None)

Descriptor for a service.

name: (str) Name of the service. full_name: (str) Full name of the service, including package name. index: (int) 0-indexed index giving the order that this services

definition appears within the .proto file.

methods: (list of MethodDescriptor) List of methods provided by this

service.

methods_by_name: (dict str -> MethodDescriptor) Same MethodDescriptor

objects as in |methods_by_name|, but indexed by “name” attribute in each MethodDescriptor.

options: (descriptor_pb2.ServiceOptions) Service options message or

None to use default service options.

file: (FileDescriptor) Reference to file info.

CopyToProto(proto)

Copies this to a descriptor_pb2.ServiceDescriptorProto.

Parameters

proto – An empty descriptor_pb2.ServiceDescriptorProto.

FindMethodByName(name)

Searches for the specified method, and returns its descriptor.

GetOptions()

Retrieves descriptor options.

This method returns the options set or creates the default options for the descriptor.

exception google.protobuf.descriptor.TypeTransformationError

Error transforming between python proto type and corresponding C++ type.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.