Contexts#

Contexts are classes that store and hold information about a specific event. These classes are typically not created manually but their instances are provided by the library.

class oblate.SchemaContext(schema, state=None)#

Context for a schema instance.

This class holds information about a Schema state. The instance of this class is accessed by the Schema.context attribute.

schema#

The schema that this context belongs to.

Type:

Schema

config#

The configuration of schema.

Type:

SchemaConfig

state#

An attribute to store any state data. This can be used to propagate or store important data while working with schema. By default, this is None.

Note

Use the state parameter in Schema initialization to modify the initial value of state.

is_initialized()#

Indicates whether the schema has initialized successfully.

class oblate.LoadContext(*, field, value, schema)#

Context for value deserialization.

This class holds important and useful information regarding deserialization of a value. The instance of this class is passed to fields.Field.value_load() while a field is being serialized.

field#

The field that the context belongs to.

Type:

fields.Field

schema#

The schema that the context belongs to.

Type:

Schema

value#

The raw value being deserialized.

state#

A dictionary to store any state data. This can be used to propagate or store important data while working with schema.

Type:

Dict[str, Any]

is_update()#

Indicates whether the value is being updated.

This is True when value is being updated and False when value is being initially set during schema initialization.

class oblate.DumpContext(included_fields, **kwargs)#

Context for value serialization.

This class holds important and useful information regarding serialization of a value. The instance of this class is passed to fields.Field.value_dump() while a field is being deserialized.

field#

The field that the context belongs to.

Type:

fields.Field

schema#

The schema that the context belongs to.

Type:

Schema

value#

The value being serialized.

included_fields#

The set of names of fields that are being serialized.

Type:

Set[str]

state#

A dictionary to store any state data. This can be used to propagate or store important data while working with schema.

Type:

Dict[str, Any]

class oblate.ErrorContext(*, error_code, schema, field, value=..., metadata=...)#

Context for error handling.

The instance of this class is passed to Field.format_error() method and holds information about the error.

error_code#

The error code indicating the error raised.

field#

The field that the error belongs to.

Type:

fields.Field

schema#

The schema that the error was caused from.

Type:

Schema

metadata#

The extra metadata attached to the error. This dictionary is populated by library and includes extra error information for certain error codes.

New in version 1.1.

Type:

Dict[str, Any]

get_value()#

Returns the value that caused the error.

This method will raise a ValueError if no value is associated to the error. This is only the case for the FIELD_REQUIRED error code.

Raises:

ValueError – No value associated to the error.