Python 2.7 has reached end of supportand will bedeprecatedon January 31, 2026. After deprecation, you won't be able to deploy Python 2.7applications, even if your organization previously used an organization policy tore-enable deployments of legacy runtimes. Your existing Python2.7 applications will continue to run and receive traffic after theirdeprecation date. We recommend thatyoumigrate to the latest supported version of Python.

The Field Class

The Field class is the base class for all fields defined on messages. This class cannot be extended by developers.

Field is provided by theprotorpc.messages module.

Constructor

The constructor of the Field class is defined as follows:

class Field (message_type,number, [required=False |repeated=False],variant=None,default=None)

Initializes an Field instance. Each sub-class of Field must define the following:

VARIANTS
Set of variant types accepted by that field.
DEFAULT_VARIANT
Default variant type if not specified in constructor.
Arguments
number
Number of the field. Must be unique per message class.
required=False
Whether or not this field is required. Mutually exclusive with therepeated argument; do not specifyrepeated if you userequired.
repeated=False
Whether or not this field is repeated. Mutually exclusive with therequired argument; do not specifyrequired=True if you also userepeated=True.
variant=None
Provides additional encoding information that is mainly used by protocol buffers. These variants map to the Type values indescriptor.proto. Best practice is to use default values, but you can specify values as 32-bit, 64-bit, unsigned, etc.
default=None
Default value for the field if not found in the underlying request.

Note: Default values are not permitted for repeated fields or message fields.

Raises the following exceptions:

Class Property

The FieldList class has one property:

default()
The default value for the field.

Instance Methods

FieldList instances have the following methods:

validate(value)
Validates a value assigned to a field.Arguments
value
The value to evaluate.

Raises aValidationError if the value is not an expected type.

validate_default_element(value)

Validates a value assigned to a default field. Specific to a single element.

Some fields may allow for delayed resolution of default types necessary in the case of circular definition references. In this case, the default value might be a placeholder that is resolved when needed after all the message classes are defined.

Arguments
value
Default value to validate.

Raises aValidationError if the value is not an expected type.

validate_default(value)

Validates that a field's default value.

Arguments
value
Default value to validate.

Raises aValidationError if the value is not an expected type.

message_definition()

Gets a message definition that contains this Field definition. Returns a definition for theMessage object that contains the Field. Returns None if Field is defined outside of a message class.

Raises aValidationError if the value is not an expected type.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.