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 Field Class
Stay organized with collections
Save and categorize content based on your preferences.
The Field class is the base class for all fields defined on messages. This class cannot be extended by developers.
Field
is provided by the protorpc.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 the
repeated
argument; do not specify repeated
if you use required
.
- repeated=False
- Whether or not this field is repeated. Mutually exclusive with the
required
argument; do not specify required=True
if you also use repeated=True
.
- variant=None
- Provides additional encoding information that is mainly used by protocol buffers. These variants map to the Type values in
descriptor.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 a ValidationError 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 a ValidationError 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 a ValidationError if the value is not an expected type.
- message_definition()
-
Gets a message definition that contains this Field definition. Returns a definition for the Message object that contains the Field. Returns None if Field is defined outside of a message class.
Raises a ValidationError if the value is not an expected 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-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\u003eField\u003c/code\u003e class serves as the foundation for all fields within messages and cannot be extended by developers, and is provided by the \u003ccode\u003eprotorpc.messages\u003c/code\u003e module.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eField\u003c/code\u003e constructor takes parameters like \u003ccode\u003enumber\u003c/code\u003e, \u003ccode\u003erequired\u003c/code\u003e, \u003ccode\u003erepeated\u003c/code\u003e, \u003ccode\u003evariant\u003c/code\u003e, and \u003ccode\u003edefault\u003c/code\u003e to define field properties, and raises exceptions for invalid inputs, such as an invalid default, or mutually exclusive arguments.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eField\u003c/code\u003e class contains \u003ccode\u003eVARIANTS\u003c/code\u003e and \u003ccode\u003eDEFAULT_VARIANT\u003c/code\u003e properties that define the field's acceptable variant types, and its default variant type respectively.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eField\u003c/code\u003e instance methods include \u003ccode\u003evalidate\u003c/code\u003e, \u003ccode\u003evalidate_default_element\u003c/code\u003e, \u003ccode\u003evalidate_default\u003c/code\u003e to check values against expectations, and \u003ccode\u003emessage_definition\u003c/code\u003e to find the \u003ccode\u003eMessage\u003c/code\u003e that contains the Field.\u003c/p\u003e\n"],["\u003cp\u003eDefault values for Fields are not permitted for repeated fields or message fields, as specified by the argument \u003ccode\u003edefault=None\u003c/code\u003e in the constructor.\u003c/p\u003e\n"]]],[],null,["# The Field Class\n\nThe Field class is the base class for all fields defined on messages. This class cannot be extended by developers.\n\n`Field` is provided by the `protorpc.messages` module.\n\nConstructor\n-----------\n\nThe constructor of the Field class is defined as follows:\n\nclass\nField\n(message_type,\nnumber,\n\\[required=False \\| repeated=False\\], variant=None, default=None)\n\n: Initializes an Field instance. Each sub-class of Field must define the following:\n\n `VARIANTS`\n : Set of variant types accepted by that field.\n\n `DEFAULT_VARIANT`\n : Default variant type if not specified in constructor.\n **Arguments**\n\n number\n : Number of the field. Must be unique per message class.\n\n required=False\n : Whether or not this field is required. Mutually exclusive with the `repeated` argument; do not specify `repeated` if you use `required`.\n\n repeated=False\n : Whether or not this field is repeated. Mutually exclusive with the `required` argument; do not specify `required=True` if you also use `repeated=True`.\n\n variant=None\n : Provides additional encoding information that is mainly used by protocol buffers. These variants map to the Type values in `descriptor.proto`. Best practice is to use default values, but you can specify values as 32-bit, 64-bit, unsigned, etc.\n\n default=None\n\n : Default value for the field if not found in the underlying request. **Note:** Default values are not permitted for repeated fields or message fields.\n\n Raises the following exceptions:\n\n - [InvalidVariantError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#InvalidVariantError) when invalid variant for field is provided.\n - [InvalidDefaultError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#InvalidDefaultError) when invalid default for field is provided.\n - [FieldDefinitionError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#FieldDefinitionError) when invalid number provided or mutually exclusive fields are used.\n - [InvalidNumberError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#InvalidNumberError) when the field number is out of range or reserved.\n\nClass Property\n--------------\n\nThe FieldList class has one property:\n\ndefault()\n: The default value for the field.\n\nInstance Methods\n----------------\n\nFieldList instances have the following methods:\n\nvalidate(value)\n: Validates a value assigned to a field.\n **Arguments**\n\n value\n : The value to evaluate.\n\n Raises a [ValidationError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#ValidationError) if the value is not an expected type.\n\nvalidate_default_element(value)\n\n: Validates a value assigned to a default field. Specific to a single element.\n\n 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.\n **Arguments**\n\n value\n : Default value to validate.\n\n Raises a [ValidationError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#ValidationError) if the value is not an expected type.\n\nvalidate_default(value)\n\n: Validates that a field's default value.\n\n **Arguments**\n\n value\n : Default value to validate.\n\n Raises a [ValidationError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#ValidationError) if the value is not an expected type.\n\nmessage_definition()\n\n: Gets a message definition that contains this Field definition. Returns a definition for the [Message](/appengine/docs/legacy/standard/python/tools/protorpc/messages/messageclass) object that contains the Field. Returns None if Field is defined outside of a message class.\n\n Raises a [ValidationError](/appengine/docs/legacy/standard/python/tools/protorpc/messages/exceptions#ValidationError) if the value is not an expected type."]]