The EnumField Class

The EnumField class provides definitions for enum values. Enum fields may have default values that are delayed until the associated enum type is resolved. This is necessary to support certain circular references. For example:

from protorpc import messages

class Message1(messages.Message):

    class Color(messages.Enum):
        RED = 1
        GREEN = 2
        BLUE = 3

    # Validate this field's default value when default is accessed.
    animal = messages.EnumField('Message2.Animal', 1, default='HORSE')

class Message2(messages.Message):

    class Animal(messages.Enum):
        DOG = 1
        CAT = 2
        HORSE = 3

    # This fields default value will be validated right away since Color is
    # already fully resolved.
    color = messages.EnumField(Message1.Color, 1, default='RED')

EnumField is provided by the protorpc.messages module.

Constructor

The constructor of the EnumField class is defined as follows:

class EnumField(enum_type, number, required, repeated, variant, default)

Provides a field definition for Enum values.

Arguments
enum_type
The Enum type for a field. Must be a subclass of Enum.
number
The number of the field. Must be unique per message class.
required
Whether or not this field is required. Mutually exclusive with the repeated argument; do not specify repeated if you use required.
repeated
Whether or not this field is repeated. Mutually exclusive with the required argument; do not specify required if you use repeated.
variant
Further specifies the type of field. Some field types are further restrained based on the underlying wire format. Best practice is to use the default value, but developers can use this field to declare an integer field as a 32-bit integer vs. the default 64 bit.
default
Default value to use for the field if it is not found in stream.

Raises a FieldDefinitionError when enum_type is invalid.

Class Properties

The EnumField class provides the following class properties:

type()
Enum type used for the field.
default()
Default for the enum field. If the default value is unresolved, uses Enum type as the default.

Instance Methods

EnumField instances have the following method:

validate_default_element(value)
Validates the default element of the Enum field. Enum fields allow for delayed resolution of default values when the type of the field has not been resolved. The default value of a field may be a string or an integer. If the Enum type of the field has been resolved, the default value is validated against that type.