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_pool

Provides DescriptorPool to use as a container for proto2 descriptors.

The DescriptorPool is used in conjection with a DescriptorDatabase to maintain a collection of protocol buffer descriptors for use when dynamically creating message types at runtime.

For most applications protocol buffers should be used via modules generated by the protocol buffer compiler tool. This should only be used when the type of protocol buffers used in an application or library cannot be predetermined.

Below is a straightforward example on how to use this class:

pool = DescriptorPool()
file_descriptor_protos = [ ... ]
for file_descriptor_proto in file_descriptor_protos:
  pool.Add(file_descriptor_proto)
my_message_descriptor = pool.FindMessageTypeByName('some.package.MessageType')

The message descriptor can be used in conjunction with the message_factory module in order to create a protocol buffer class that can be encoded and decoded.

If you want to get a Python class for the specified proto, use the helper functions inside google.protobuf.message_factory directly instead of this class.

google.protobuf.descriptor_pool.Default()
class google.protobuf.descriptor_pool.DescriptorPool(descriptor_db=None)

A collection of protobufs dynamically constructed by descriptor protos.

Add(file_desc_proto)

Adds the FileDescriptorProto and its types to this pool.

Parameters

file_desc_proto (FileDescriptorProto) – The file descriptor to add.

AddDescriptor(**kwargs)
AddEnumDescriptor(**kwargs)
AddExtensionDescriptor(**kwargs)
AddFileDescriptor(**kwargs)
AddSerializedFile(serialized_file_desc_proto)

Adds the FileDescriptorProto and its types to this pool.

Parameters

serialized_file_desc_proto (bytes) – A bytes string, serialization of the FileDescriptorProto to add.

AddServiceDescriptor(**kwargs)
FindAllExtensions(message_descriptor)

Gets all the known extensions of a given message.

Extensions have to be registered to this pool by calling Add() or AddExtensionDescriptor().

Parameters

message_descriptor (Descriptor) – Descriptor of the extended message.

Returns

Field descriptors describing the extensions.

Return type

list[FieldDescriptor]

FindEnumTypeByName(full_name)

Loads the named enum descriptor from the pool.

Parameters

full_name (str) – The full name of the enum descriptor to load.

Returns

The enum descriptor for the named type.

Return type

EnumDescriptor

Raises

KeyError – if the enum cannot be found in the pool.

FindExtensionByName(full_name)

Loads the named extension descriptor from the pool.

Parameters

full_name (str) – The full name of the extension descriptor to load.

Returns

The field descriptor for the named extension.

Return type

FieldDescriptor

Raises

KeyError – if the extension cannot be found in the pool.

FindExtensionByNumber(message_descriptor, number)

Gets the extension of the specified message with the specified number.

Extensions have to be registered to this pool by calling Add() or AddExtensionDescriptor().

Parameters
  • message_descriptor (Descriptor) – descriptor of the extended message.

  • number (int) – Number of the extension field.

Returns

The descriptor for the extension.

Return type

FieldDescriptor

Raises

KeyError – when no extension with the given number is known for the specified message.

FindFieldByName(full_name)

Loads the named field descriptor from the pool.

Parameters

full_name (str) – The full name of the field descriptor to load.

Returns

The field descriptor for the named field.

Return type

FieldDescriptor

Raises

KeyError – if the field cannot be found in the pool.

FindFileByName(file_name)

Gets a FileDescriptor by file name.

Parameters

file_name (str) – The path to the file to get a descriptor for.

Returns

The descriptor for the named file.

Return type

FileDescriptor

Raises

KeyError – if the file cannot be found in the pool.

FindFileContainingSymbol(symbol)

Gets the FileDescriptor for the file containing the specified symbol.

Parameters

symbol (str) – The name of the symbol to search for.

Returns

Descriptor for the file that contains the specified symbol.

Return type

FileDescriptor

Raises

KeyError – if the file cannot be found in the pool.

FindMessageTypeByName(full_name)

Loads the named descriptor from the pool.

Parameters

full_name (str) – The full name of the descriptor to load.

Returns

The descriptor for the named type.

Return type

Descriptor

Raises

KeyError – if the message cannot be found in the pool.

FindMethodByName(full_name)

Loads the named service method descriptor from the pool.

Parameters

full_name (str) – The full name of the method descriptor to load.

Returns

The method descriptor for the service method.

Return type

MethodDescriptor

Raises

KeyError – if the method cannot be found in the pool.

FindOneofByName(full_name)

Loads the named oneof descriptor from the pool.

Parameters

full_name (str) – The full name of the oneof descriptor to load.

Returns

The oneof descriptor for the named oneof.

Return type

OneofDescriptor

Raises

KeyError – if the oneof cannot be found in the pool.

FindServiceByName(full_name)

Loads the named service descriptor from the pool.

Parameters

full_name (str) – The full name of the service descriptor to load.

Returns

The service descriptor for the named service.

Return type

ServiceDescriptor

Raises

KeyError – if the service cannot be found in the pool.