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
,
create_key=None
)
Descriptor for a protocol message type.
name
Name of this protocol message type.
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
:
Descriptor
FieldDescriptor
objects as in
fields
, but indexed
by “number” attribute in each FieldDescriptor.
Type
:
dict
(
int
,
FieldDescriptor
)
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
)
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
)
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
,
create_key=None
)
Descriptor for an enum defined in a .proto file.
name
Name of the enum type.
Type
:
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
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
,
create_key=None
)
Descriptor for a single value within an enum.
name
Name of this value.
Type
:
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 by
EnumDescriptor
’s constructor if we’re
passed into one.
Type
:
EnumDescriptor
GetOptions
(
)
Retrieves descriptor options.
This method returns the options set or creates the default options for the
descriptor.
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
,
create_key=None
)
Descriptor for a single field in a .proto file.
name
Name of this field, exactly as it appears in .proto.
Type
:
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
:
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
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
containing_oneof
If the field is a member of a oneof
union, contains its descriptor. Otherwise, None.
Type
:
OneofDescriptor
GetOptions
(
)
Retrieves descriptor options.
This method returns the options set or creates the default options for the
descriptor.
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.
property
has_presence
Whether the field distinguishes between unpopulated and default values.
Raises
:
RuntimeError
– singular field that is not linked with message nor file.
class
google.protobuf.descriptor.
FileDescriptor
(
name
,
package
,
options=None
,
serialized_options=None
,
serialized_pb=None
,
dependencies=None
,
public_dependencies=None
,
syntax=None
,
pool=None
,
create_key=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
:
extensions_by_name
Mapping from extension
names declared at file scope to their
FieldDescriptor
.
Type
:
dict
(
str
,
FieldDescriptor
)
pool
The pool this descriptor belongs to. When not
passed to the constructor, the global default pool is used.
Type
:
DescriptorPool
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
,
client_streaming=False
,
server_streaming=False
,
options=None
,
serialized_options=None
,
create_key=None
)
Descriptor for a method in a service.
name
Name of the method within the service.
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
,
create_key=None
)
Descriptor for a oneof field.
name
Name of the oneof field.
Type
:
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
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
,
create_key=None
)
Descriptor for a service.
name
Name of the service.
Type
:
index
0-indexed index giving the order that this services
definition appears within the .proto file.
Type
:
MethodDescriptor
objects as in
methods_by_name
, but
indexed by “name” attribute in each
MethodDescriptor
.
Type
:
dict
(
str
,
MethodDescriptor
)
FindMethodByName
(
name
)
Searches for the specified method, and returns its descriptor.
Parameters
:
name
(
str
) – Name of the method.
Returns
:
the descriptor 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
google.protobuf.descriptor
google.protobuf.descriptor_database
google.protobuf.descriptor_pb2
google.protobuf.descriptor_pool
google.protobuf.duration_pb2
google.protobuf.empty_pb2
google.protobuf.field_mask_pb2
google.protobuf.internal.containers
google.protobuf.json_format
google.protobuf.message
google.protobuf.message_factory
google.protobuf.proto_builder
google.protobuf.reflection
google.protobuf.service
google.protobuf.service_reflection
google.protobuf.struct_pb2
google.protobuf.symbol_database
google.protobuf.text_encoding
google.protobuf.text_format
google.protobuf.timestamp_pb2
google.protobuf.type_pb2
google.protobuf.unknown_fields
google.protobuf.wrappers_pb2
Related Topics
Documentation overview