Python 2.7 has reached end of support
and will be
deprecated
on January 31, 2026. After deprecation, you won't be able to deploy Python 2.7
applications, even if your organization previously used an organization policy to
re-enable deployments of legacy runtimes. Your existing Python
2.7 applications will continue to run and receive traffic after their
deprecation date. We recommend that
you
migrate to the latest supported version of Python.
Descriptor Module Functions
Stay organized with collections
Save and categorize content based on your preferences.
The descriptor.py
module contains message definitions and functions for converting Google Protocol RPC definitions into a transmittable message format.
Describing an Enum instance, Enum class, Field class, or Message class generates an appropriate descriptor object. A descriptor object is an object that describes other Google Protocol RPC definitions such as enums, messages, and services. You can use this message to transmit information to clients wishing to know the description of an enum value, enum, field or message without needing to download the source code. This format is also compatible with other, non-Python languages.
The descriptors are modeled to be binary compatible with https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto
Note! The names of types and fields defined in descriptor.py
do not necessarily match those defined in descriptor.proto
. Google Protocol RPC is designed this way to make source code files that use these descriptors easier to read. For example, FieldDescriptors need to be prefixed with TYPE
in descriptor.proto
, but not in descriptor.py
.
The following code sample demonstrates use of the descriptor module to describe a class called Pixel
using the MessageDescriptor
class.
from protorpc import descriptor
from protorpc import messages
class Pixel(messages.Message):
x = messages.IntegerField(1, required=True)
y = messages.IntegerField(2, required=True)
color = messages.BytesField(3)
# Describe Pixel class using message descriptor.
fields = [
descriptor.FieldDescriptor(name='x',
number=1,
label=descriptor.FieldDescriptor.Label.REQUIRED,
variant=descriptor.FieldDescriptor.Variant.INT64),
descriptor.FieldDescriptor(name='y',
number=2,
label=descriptor.FieldDescriptor.Label.REQUIRED,
variant=descriptor.FieldDescriptor.Variant.INT64),
descriptor.FieldDescriptor(name='color',
number = 3,
label=descriptor.FieldDescriptor.Label.OPTIONAL,
variant=descriptor.FieldDescriptor.Variant.BYTES)]
message = descriptor.MessageDescriptor(name='Pixel',
fields=fields)
# Describing is the equivalent of building the above message.
message == descriptor.describe_message(Pixel)
The protorpc.descriptor
package provides the following functions:
- describe(value)
-
Describes any value as a descriptor. You can use this helper function to describe any object with an appropriate descriptor object.
Arguments
- value
- Value to describe as a descriptor.
Returns a descriptor if the object is describable as a descriptor, else None.
- describe_enum(enum_definition)
-
Builds a descriptor from an Enum class.
Arguments
- enum_value
- The Enum class to describe.
Returns an initialized EnumDescriptor instance describing the Enum instance.
EnumDescriptor Class fields:
name
- Name of the specified Enum without any qualifications.
values
- Values defined by the Enum class.
- describe_enum_value(enum_value)
-
Builds a descriptor from an Enum instance.
Arguments
- enum_value
- The Enum value to describe.
Returns an initialized EnumValueDescriptor instance describing the Enum instance.
EnumValueDescriptor Class fields:
name
- Name of the enumeration value.
number
- Number of the enumeration value.
- describe_field(field_definition)
-
Builds a descriptor from a Field instance.
Arguments
- field_definition
- The Field instance to describe.
Returns an initialized FieldDescriptor instance describing the Field instance with a list of Enums and fields.
FieldDescriptor Class Enums:
Variant
- Wire format hint subtypes for the specified field.
Label
- Values for optional, required, and repeated fields.
FieldDescriptor Class fields:
name
- Name of the specified field.
number
- Number of the specified field.
variant
- Variant of the specified field.
type_name
- Type name for the message and enum fields.
default_value
- String representation of the default value.
- describe_file(module)
-
Builds a file from a specified Python module.
Arguments
- module
- The Python to describe.
Returns an initialized FileDescriptor instance describing the module.
FileDescriptor Class fields
package
- Fully qualified name of the package that the definitions belong to.
message_types
- Message definitions contained in the file.
enum_types
- Enum definitions contained in the file.
service_types
- Service definitions contained in the file.
- describe_file_set(modules)
-
Builds a file set from the specified Python modules.
Arguments
- modules
- Iterable of the Python module to describe.
Returns an initialized FileSet instance describing the module.
FileSet Class fields:
files
- Files in the file set.
- describe_message(message_definition)
-
Builds a descriptor from a Message class.
Arguments
- message_definition
- The Message class to describe.
Returns an initialized MessageDescriptor instance describing the Message class.
MessageDescriptor Class fields:
name
- Fully qualified name of the package that the definitions belong to.
fields
- Fields defined for the message.
message_types
- Nested Message classes defined on the message.
enum_types
- Nested Enum classes defined on the message.
- describe_method(method)
-
Builds a descriptor from a remote service method.
Arguments
- method
- The remote service method to describe.
Returns an initialized MethodDescriptor instance describing the remote service method.
MethodDescriptor fields:
name
- Name of the service method.
request_type
- Fully qualified name or relative name of the request message type.
response_type
- Fully qualified or relative name of the response message type.
- describe_service(service_class)
-
Builds a descriptor from a Service class.
Arguments
- service_class
- The Service class to describe.
Returns an initialized ServiceDescriptor instance describing the service.
ServiceDescriptor fields:
name
- Unqualified name of the Service.
methods
- Remote methods of the Service.
- import_descriptor_loader(definition_name, importer=__import__ )
-
Finds objects by importing modules as needed. A definition loader is a function that resolves a definition name to a descriptor. The import finder resolves definitions to their names by importing modules when necessary.
Arguments
- definition_name
- Name of the definition to find.
- importer=__import__ )
- Import function used for importing new modules.
Returns an appropriate descriptor from any describable type located by name.
Raises a DefinitionNotFoundError when a name does not refer to either a definition or a module.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-29 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-29 UTC."],[[["\u003cp\u003eThe \u003ccode\u003edescriptor.py\u003c/code\u003e module is used to convert Google Protocol RPC definitions into a transmittable message format, allowing for the description of enums, messages, and services.\u003c/p\u003e\n"],["\u003cp\u003eDescriptor objects can be created for Enum instances, Enum classes, Field classes, and Message classes, allowing transmission of information about these definitions without needing the source code.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003edescribe()\u003c/code\u003e function serves as a helper for generating descriptors for various objects, and functions like \u003ccode\u003edescribe_enum()\u003c/code\u003e, \u003ccode\u003edescribe_message()\u003c/code\u003e, and \u003ccode\u003edescribe_field()\u003c/code\u003e are used to describe specific types of objects.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eimport_descriptor_loader\u003c/code\u003e function is able to find objects by importing modules as needed and resolve a definition name to a descriptor, returning an appropriate descriptor from any describable type located by name.\u003c/p\u003e\n"],["\u003cp\u003eThe module defines several descriptor classes, such as \u003ccode\u003eFileDescriptor\u003c/code\u003e, \u003ccode\u003eMessageDescriptor\u003c/code\u003e, and \u003ccode\u003eFieldDescriptor\u003c/code\u003e, which contain information like names, types, and values, mirroring the structure of Google Protocol RPC definitions.\u003c/p\u003e\n"]]],[],null,["# Descriptor Module Functions\n\nThe `descriptor.py` module contains message definitions and functions for converting Google Protocol RPC definitions into a transmittable message format.\n\nDescribing an Enum instance, Enum class, Field class, or Message class generates an appropriate descriptor object. A descriptor object is an object that describes other Google Protocol RPC definitions such as enums, messages, and services. You can use this message to transmit information to clients wishing to know the description of an enum value, enum, field or message without needing to download the source code. This format is also compatible with other, non-Python languages.\n\nThe descriptors are modeled to be binary compatible with `https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto`\n\n**Note!** The names of types and fields defined in `descriptor.py` do not necessarily match those defined in `descriptor.proto`. Google Protocol RPC is designed this way to make source code files that use these descriptors easier to read. For example, FieldDescriptors need to be prefixed with `TYPE` in `descriptor.proto`, but not in `descriptor.py`.\n\nThe following code sample demonstrates use of the descriptor module to describe a class called `Pixel` using the `MessageDescriptor` class. \n\n```python\nfrom protorpc import descriptor\nfrom protorpc import messages\n\nclass Pixel(messages.Message):\n x = messages.IntegerField(1, required=True)\n y = messages.IntegerField(2, required=True)\n\n color = messages.BytesField(3)\n\n# Describe Pixel class using message descriptor. \nfields = [\n descriptor.FieldDescriptor(name='x', \n number=1,\n label=descriptor.FieldDescriptor.Label.REQUIRED,\n variant=descriptor.FieldDescriptor.Variant.INT64),\n\n descriptor.FieldDescriptor(name='y', \n number=2,\n label=descriptor.FieldDescriptor.Label.REQUIRED,\n variant=descriptor.FieldDescriptor.Variant.INT64),\n\n descriptor.FieldDescriptor(name='color',\n number = 3,\n label=descriptor.FieldDescriptor.Label.OPTIONAL,\n variant=descriptor.FieldDescriptor.Variant.BYTES)]\n\nmessage = descriptor.MessageDescriptor(name='Pixel',\n fields=fields)\n\n# Describing is the equivalent of building the above message.\nmessage == descriptor.describe_message(Pixel)\n```\n\nThe `protorpc.descriptor` package provides the following functions:\n\ndescribe(value)\n\n: Describes any value as a descriptor. You can use this helper function to describe any object with an appropriate descriptor object.\n\n **Arguments**\n\n value\n : Value to describe as a descriptor.\n\n Returns a descriptor if the object is describable as a descriptor, else None.\n\ndescribe_enum(enum_definition)\n\n: Builds a descriptor from an Enum class.\n\n **Arguments**\n\n enum_value\n : The Enum class to describe.\n\n Returns an initialized EnumDescriptor instance describing the Enum instance.\n\n EnumDescriptor Class fields:\n\n `name`\n : Name of the specified Enum without any qualifications.\n\n `values`\n : Values defined by the Enum class.\n\ndescribe_enum_value(enum_value)\n\n: Builds a descriptor from an Enum instance.\n **Arguments**\n\n enum_value\n : The Enum value to describe.\n\n Returns an initialized EnumValueDescriptor instance describing the Enum instance.\n\n EnumValueDescriptor Class fields:\n\n `name`\n : Name of the enumeration value.\n\n `number`\n : Number of the enumeration value.\n\ndescribe_field(field_definition)\n\n: Builds a descriptor from a Field instance.\n\n **Arguments**\n\n field_definition\n : The Field instance to describe.\n\n Returns an initialized FieldDescriptor instance describing the Field instance with a list of Enums and fields.\n\n FieldDescriptor Class Enums:\n\n `Variant`\n : Wire format hint subtypes for the specified field.\n\n `Label`\n : Values for optional, required, and repeated fields.\n\n FieldDescriptor Class fields:\n\n `name`\n : Name of the specified field.\n\n `number`\n : Number of the specified field.\n\n `variant`\n : Variant of the specified field.\n\n `type_name`\n : Type name for the message and enum fields.\n\n `default_value`\n : String representation of the default value.\n\ndescribe_file(module)\n\n: Builds a file from a specified Python module.\n\n **Arguments**\n\n module\n : The Python to describe.\n\n Returns an initialized FileDescriptor instance describing the module.\n\n FileDescriptor Class fields\n\n `package`\n : Fully qualified name of the package that the definitions belong to.\n\n `message_types`\n : Message definitions contained in the file.\n\n `enum_types`\n : Enum definitions contained in the file.\n\n `service_types`\n : Service definitions contained in the file.\n\ndescribe_file_set(modules)\n\n: Builds a file set from the specified Python modules.\n\n **Arguments**\n\n modules\n : Iterable of the Python module to describe.\n\n Returns an initialized FileSet instance describing the module.\n\n FileSet Class fields:\n\n `files`\n : Files in the file set.\n\ndescribe_message(message_definition)\n\n: Builds a descriptor from a Message class.\n\n **Arguments**\n\n message_definition\n : The Message class to describe.\n\n Returns an initialized MessageDescriptor instance describing the Message class.\n\n MessageDescriptor Class fields:\n\n `name`\n : Fully qualified name of the package that the definitions belong to.\n\n `fields`\n : Fields defined for the message.\n\n `message_types`\n : Nested Message classes defined on the message.\n\n `enum_types`\n : Nested Enum classes defined on the message.\n\ndescribe_method(method)\n\n: Builds a descriptor from a remote service method.\n\n **Arguments**\n\n method\n : The remote service method to describe.\n\n Returns an initialized MethodDescriptor instance describing the remote service method.\n\n MethodDescriptor fields:\n\n `name`\n : Name of the service method.\n\n `request_type`\n : Fully qualified name or relative name of the request message type.\n\n `response_type`\n : Fully qualified or relative name of the response message type.\n\ndescribe_service(service_class)\n\n: Builds a descriptor from a Service class.\n\n **Arguments**\n\n service_class\n : The Service class to describe.\n\n Returns an initialized ServiceDescriptor instance describing the service.\n\n ServiceDescriptor fields:\n\n `name`\n : Unqualified name of the Service.\n\n `methods`\n : Remote methods of the Service.\n\nimport_descriptor_loader(definition_name, importer=__import__ )\n\n: Finds objects by importing modules as needed. A definition loader is a function that resolves a definition name to a descriptor. The import finder resolves definitions to their names by importing modules when necessary.\n\n **Arguments**\n\n definition_name\n : Name of the definition to find.\n\n importer=__import__ )\n : Import function used for importing new modules.\n\n Returns an appropriate descriptor from any describable type located by name.\n\n Raises a [DefinitionNotFoundError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#DefinitionNotFoundError) when a name does not refer to either a definition or a module."]]