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.
The EnumField Class
Stay organized with collections
Save and categorize content based on your preferences.
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.
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-07 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-07 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eEnumField\u003c/code\u003e class defines fields for enum values, supporting delayed default value resolution to handle circular references.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eEnumField\u003c/code\u003e is constructed using the enum type, field number, and optional arguments like \u003ccode\u003erequired\u003c/code\u003e, \u003ccode\u003erepeated\u003c/code\u003e, \u003ccode\u003evariant\u003c/code\u003e, and \u003ccode\u003edefault\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003etype()\u003c/code\u003e property returns the Enum type associated with the field, while \u003ccode\u003edefault()\u003c/code\u003e provides the default enum value, or the enum type if the value is unresolved.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003evalidate_default_element(value)\u003c/code\u003e is used to validate the default value, supporting strings or integers, against the resolved Enum type.\u003c/p\u003e\n"],["\u003cp\u003eThe default value for an \u003ccode\u003eEnumField\u003c/code\u003e can be validated at the time it is accessed, and it is provided by the \u003ccode\u003eprotorpc.messages\u003c/code\u003e module.\u003c/p\u003e\n"]]],[],null,[]]