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.
-
full_name
¶ Fully-qualified name of this protocol message type, which will include protocol “package” name and the name of any enclosing types.
- Type
-
containing_type
¶ Reference to the descriptor of the type containing us, or None if this is top-level.
- Type
-
fields
¶ Field descriptors for all fields in this type.
- Type
-
fields_by_number
¶ Same
FieldDescriptor
objects as infields
, but indexed by “number” attribute in each FieldDescriptor.- Type
-
fields_by_name
¶ Same
FieldDescriptor
objects as infields
, but indexed by “name” attribute in eachFieldDescriptor
.- Type
-
nested_types
¶ Descriptor references for all protocol message types nested within this one.
- Type
-
nested_types_by_name
¶ Same Descriptor objects as in
nested_types
, but indexed by “name” attribute in each Descriptor.- Type
-
enum_types
¶ EnumDescriptor
references for all enums contained within this type.- Type
-
enum_types_by_name
¶ Same
EnumDescriptor
objects as inenum_types
, but indexed by “name” attribute in each EnumDescriptor.- Type
-
enum_values_by_name
¶ Dict mapping from enum value name to
EnumValueDescriptor
for that value.- Type
-
extensions
¶ All extensions defined directly within this message type (NOT within a nested type).
- Type
-
extensions_by_name
¶ Same FieldDescriptor objects as
extensions
, but indexed by “name” attribute of each FieldDescriptor.- Type
-
oneofs
¶ The list of descriptors for oneof fields in this message.
- Type
-
file
¶ Reference to file descriptor.
- Type
-
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 byFieldDescriptor.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.
-
values_by_number
¶ Same as
values
, but indexed by the “number” field of each EnumValueDescriptor.- Type
-
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
-
file
¶ Reference to file descriptor.
- Type
-
options
¶ Enum options message or None to use default enum options.
-
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.
-
index
¶ Dense, 0-indexed index giving the order that this value appears textually within its enum in the .proto file.
- Type
-
type
¶ EnumDescriptor
to which this value belongs. Set byEnumDescriptor
’s constructor if we’re passed into one.- Type
-
options
¶ 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.
-
full_name
¶ Name of this field, including containing scope. This is particularly relevant for extensions.
- Type
-
index
¶ Dense, 0-indexed index giving the order that this field textually appears within its message in the .proto file.
- Type
-
label
¶ (One of the LABEL_* constants below) Tells whether this field is optional, required, or repeated.
- Type
-
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
-
message_type
¶ If a composite field, a descriptor of the message type contained in this field. Otherwise, this is None.
- Type
-
enum_type
¶ If this field contains an enum, a descriptor of that enum. Otherwise, this is None.
- Type
-
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
-
options
¶ Protocol message field options or None to use default field options.
-
containing_oneof
¶ If the field is a member of a oneof union, contains its descriptor. Otherwise, None.
- Type
-
file
¶ Reference to file descriptor.
- Type
-
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
- 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¶
-
-
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
, anddependencies
fields are only set by thegoogle.protobuf.message_factory
module, and not by the generated proto code.-
dependencies
¶ List of other
FileDescriptor
objects thisFileDescriptor
depends on.- Type
-
public_dependencies
¶ A subset of
dependencies
, which were declared as “public”.- Type
-
message_types_by_name
¶ Mapping from message names to their
Desctiptor
.- Type
-
enum_types_by_name
¶ Mapping from enum names to their
EnumDescriptor
.- Type
-
extensions_by_name
¶ Mapping from extension names declared at file scope to their
FieldDescriptor
.- Type
-
services_by_name
¶ Mapping from services’ names to their
ServiceDescriptor
.- Type
-
pool
¶ The pool this descriptor belongs to. When not passed to the constructor, the global default pool is used.
- Type
-
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.
-
containing_service
¶ The service that contains this method.
- Type
-
input_type
¶ The descriptor of the message that this method accepts.
- Type
-
output_type
¶ The descriptor of the message that this method returns.
- Type
-
options
¶ Method options message, or None to use default method options.
- Type
-
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.
-
containing_type
¶ Descriptor
of the protocol message type that contains this field. Set by theDescriptor
constructor if we’re passed into one.- Type
-
fields
¶ The list of field descriptors this oneof can contain.
- Type
-
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.
-
index
¶ 0-indexed index giving the order that this services definition appears within the .proto file.
- Type
-
methods
¶ List of methods provided by this service.
- Type
-
methods_by_name
¶ Same
MethodDescriptor
objects as inmethods_by_name
, but indexed by “name” attribute in eachMethodDescriptor
.- Type
-
options
¶ Service options message or None to use default service options.
-
file
¶ Reference to file info.
- Type
-
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
-
GetOptions
()¶ Retrieves descriptor options.
This method returns the options set or creates the default options for the descriptor.
-