Warning

You are reading the documentation for the latest committed changes of the Protocol Buffers package for Python. Some features may not yet be released. Read the documentation for the latest released package at googleapis.dev.

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.

name

Name of this protocol message type.

Type

str

full_name

Fully-qualified name of this protocol message type, which will include protocol “package” name and the name of any enclosing types.

Type

str

containing_type

Reference to the descriptor of the type containing us, or None if this is top-level.

Type

Descriptor

fields

Field descriptors for all fields in this type.

Type

list[FieldDescriptor]

fields_by_number

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

Type

dict(int, FieldDescriptor)

fields_by_name

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

Type

dict(str, FieldDescriptor)

nested_types

Descriptor references for all protocol message types nested within this one.

Type

list[Descriptor]

nested_types_by_name

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

Type

dict(str, Descriptor)

enum_types

EnumDescriptor references for all enums contained within this type.

Type

list[EnumDescriptor]

enum_types_by_name

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

Type

dict(str, EnumDescriptor)

enum_values_by_name

Dict mapping from enum value name to EnumValueDescriptor for that value.

Type

dict(str, EnumValueDescriptor)

extensions

All extensions defined directly within this message type (NOT within a nested type).

Type

list[FieldDescriptor]

extensions_by_name

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

Type

dict(str, FieldDescriptor)

is_extendable

Does this type define any extension ranges?

Type

bool

oneofs

The list of descriptors for oneof fields in this message.

Type

list[OneofDescriptor]

oneofs_by_name

Same objects as in oneofs, but indexed by “name” attribute.

Type

dict(str, OneofDescriptor)

file

Reference to file descriptor.

Type

FileDescriptor

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

Same FieldDescriptor objects as in fields, but indexed by FieldDescriptor.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.

name

Name of the enum type.

Type

str

full_name

Full name of the type, including package name and any enclosing type(s).

Type

str

values

List of the values in this enum.

Type

list[EnumValueDescriptors]

values_by_name

Same as values, but indexed by the “name” field of each EnumValueDescriptor.

Type

dict(str, EnumValueDescriptor)

values_by_number

Same as values, but indexed by the “number” field of each EnumValueDescriptor.

Type

dict(int, EnumValueDescriptor)

containing_type

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.

Type

Descriptor

file

Reference to file descriptor.

Type

FileDescriptor

options

Enum options message or None to use default enum options.

Type

descriptor_pb2.EnumOptions

CopyToProto(proto)

Copies this to a descriptor_pb2.EnumDescriptorProto.

Parameters

proto (descriptor_pb2.EnumDescriptorProto) – An empty descriptor proto.

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

Name of this value.

Type

str

index

Dense, 0-indexed index giving the order that this value appears textually within its enum in the .proto file.

Type

int

number

Actual number assigned to this enum value.

Type

int

type

EnumDescriptor to which this value belongs. Set by EnumDescriptor’s constructor if we’re passed into one.

Type

EnumDescriptor

options

Enum value options message or None to use default enum value options options.

Type

descriptor_pb2.EnumValueOptions

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.

name

Name of this field, exactly as it appears in .proto.

Type

str

full_name

Name of this field, including containing scope. This is particularly relevant for extensions.

Type

str

index

Dense, 0-indexed index giving the order that this field textually appears within its message in the .proto file.

Type

int

number

Tag number declared for this field in the .proto file.

Type

int

type

(One of the TYPE_* constants below) Declared type.

Type

int

cpp_type

(One of the CPPTYPE_* constants below) C++ type used to represent this field.

Type

int

label

(One of the LABEL_* constants below) Tells whether this field is optional, required, or repeated.

Type

int

has_default_value

True if this field has a default value defined, otherwise false.

Type

bool

default_value

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.

Type

Varies

containing_type

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

Type

Descriptor

message_type

If a composite field, a descriptor of the message type contained in this field. Otherwise, this is None.

Type

Descriptor

enum_type

If this field contains an enum, a descriptor of that enum. Otherwise, this is None.

Type

EnumDescriptor

is_extension

True iff this describes an extension field.

extension_scope

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.

Type

Descriptor

options

Protocol message field options or None to use default field options.

Type

descriptor_pb2.FieldOptions

containing_oneof

If the field is a member of a oneof union, contains its descriptor. Otherwise, None.

Type

OneofDescriptor

file

Reference to file descriptor.

Type

FileDescriptor

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.

Return type

int

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

Camelcase name of this field.

Returns

the name in CamelCase.

Return type

str

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 google.protobuf.message_factory module, and not by the generated proto code.

name

Name of file, relative to root of source tree.

Type

str

package

Name of the package

Type

str

syntax

string indicating syntax of the file (can be “proto2” or “proto3”)

Type

str

serialized_pb

Byte string of serialized descriptor_pb2.FileDescriptorProto.

Type

bytes

dependencies

List of other FileDescriptor objects this FileDescriptor depends on.

Type

list[FileDescriptor]

public_dependencies

A subset of dependencies, which were declared as “public”.

Type

list[FileDescriptor]

message_types_by_name

Mapping from message names to their Desctiptor.

Type

dict(str, Descriptor)

enum_types_by_name

Mapping from enum names to their EnumDescriptor.

Type

dict(str, EnumDescriptor)

extensions_by_name

Mapping from extension names declared at file scope to their FieldDescriptor.

Type

dict(str, FieldDescriptor)

services_by_name

Mapping from services’ names to their ServiceDescriptor.

Type

dict(str, ServiceDescriptor)

pool

The pool this descriptor belongs to. When not passed to the constructor, the global default pool is used.

Type

DescriptorPool

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

Name of the method within the service.

Type

str

full_name

Full name of method.

Type

str

index

0-indexed index of the method inside the service.

Type

int

containing_service

The service that contains this method.

Type

ServiceDescriptor

input_type

The descriptor of the message that this method accepts.

Type

Descriptor

output_type

The descriptor of the message that this method returns.

Type

Descriptor

options

Method options message, or None to use default method options.

Type

descriptor_pb2.MethodOptions or None

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

Name of the oneof field.

Type

str

full_name

Full name of the oneof field, including package name.

Type

str

index

0-based index giving the order of the oneof field inside its containing type.

Type

int

containing_type

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

Type

Descriptor

fields

The list of field descriptors this oneof can contain.

Type

list[FieldDescriptor]

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

Name of the service.

Type

str

full_name

Full name of the service, including package name.

Type

str

index

0-indexed index giving the order that this services definition appears within the .proto file.

Type

int

methods

List of methods provided by this service.

Type

list[MethodDescriptor]

methods_by_name

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

Type

dict(str, MethodDescriptor)

options

Service options message or None to use default service options.

Type

descriptor_pb2.ServiceOptions

file

Reference to file info.

Type

FileDescriptor

CopyToProto(proto)

Copies this to a descriptor_pb2.ServiceDescriptorProto.

Parameters

proto (descriptor_pb2.ServiceDescriptorProto) – An empty descriptor proto.

FindMethodByName(name)

Searches for the specified method, and returns its descriptor.

Parameters

name (str) – Name of the method.

Returns

the desctiptor for the requested method, if found.

Return type

MethodDescriptor or None

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.