Skip to content

BaseEncoder

Bases: ABC

Abstract base class for serialization implementations.


Attributes

Name Type Value Doc
ENCODE_ID int 0 Internal counter to ensure unique encoding ids.

This is set back to 0 if reset_encode_id=True in encode(). Internally, this is only happens in dump(). Nothing bad happens if it is not reset, but the cache ids will not start at 0 as is "standard".

assert_decode classmethod

assert_decode(fatal: bool = True)

Context manager for handling decode assertion errors.

This context manager wraps decode operations and handles AssertionError exceptions by converting them to appropriate error types based on the fatal parameter.

Parameters:

Name Type Description Default
fatal bool

If True, raises MisformedDecodableError for assertion failures. If False, raises IncorrectDecodableTypeError. Default is True.

True

Yields:

Type Description
None

Yields control to the wrapped code block.

Raises:

Type Description
MisformedDecodableError

When fatal=True and an AssertionError occurs during decoding.

IncorrectDecodableTypeError

When fatal=False and an AssertionError occurs during decoding.


decode_array abstractmethod staticmethod

decode_array(encoded: Encoded) -> EncodableArrays

Decode an encoded array back to its original form.

Parameters:

Name Type Description Default
encoded Encoded

The encoded array data to be decoded.

required

Returns:

Type Description
EncodableArrays

The decoded array in its original form.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


decode_cached_obj abstractmethod staticmethod

decode_cached_obj(encoded: Encoded, decode_cache: DecodeCache = None) -> Serializable | DeferredRef

Decode a cached object reference.

Parameters:

Name Type Description Default
encoded Encoded

The encoded representation of a cached object reference.

required
decode_cache DecodeCache

Cache used to track decoded objects and resolve references.

None

Returns:

Type Description
Serializable | DeferredRef

The decoded object or a deferred reference to be resolved later.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


decode_class abstractmethod staticmethod

decode_class(encoded: Encoded) -> type

Decode an encoded class reference back to its class object.

Parameters:

Name Type Description Default
encoded Encoded

The encoded representation of a class reference.

required

Returns:

Type Description
type

The decoded class object.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


decode_dict abstractmethod staticmethod

decode_dict(encoded: Encoded, decode_cache: DecodeCache = None) -> dict

Decode an encoded dictionary back to its original form.

Parameters:

Name Type Description Default
encoded Encoded

The encoded dictionary data to be decoded.

required
decode_cache DecodeCache

Cache used to track decoded objects and resolve references.

None

Returns:

Type Description
dict

The decoded dictionary in its original form.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


decode_function abstractmethod staticmethod

decode_function(encoded: Encoded) -> Callable

Decode an encoded function reference back to its callable form.

Parameters:

Name Type Description Default
encoded Encoded

The encoded representation of a function.

required

Returns:

Type Description
Callable

The decoded function object.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


decode_iterable abstractmethod staticmethod

decode_iterable(encoded: Encoded, decode_cache: DecodeCache = None) -> EncodableIterables

Decode an encoded iterable back to its original form.

Parameters:

Name Type Description Default
encoded Encoded

The encoded iterable data to be decoded.

required
decode_cache DecodeCache

Cache used to track decoded objects and resolve references.

None

Returns:

Type Description
EncodableIterables

The decoded iterable in its original form.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


decode_primitive abstractmethod staticmethod

decode_primitive(encoded: Encoded) -> EncodablePrimitives

Decode an encoded primitive value back to its original form.

Parameters:

Name Type Description Default
encoded Encoded

The encoded primitive value to be decoded.

required

Returns:

Type Description
EncodablePrimitives

The decoded primitive value in its original form.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


decode_uncached_obj abstractmethod staticmethod

decode_uncached_obj(encoded: Encoded, decode_cache: DecodeCache = None) -> Serializable | DeferredRef

Decode an object that was not previously cached during encoding.

Parameters:

Name Type Description Default
encoded Encoded

The encoded representation of an uncached object.

required
decode_cache DecodeCache

Cache used to track decoded objects and resolve references.

None

Returns:

Type Description
Serializable | DeferredRef

The decoded object or a deferred reference to be resolved later.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


encode_array abstractmethod staticmethod

encode_array(to_encode: EncodableArrays)

Encode an array into a serializable format.

Parameters:

Name Type Description Default
to_encode EncodableArrays

The array to be encoded.

required

Returns:

Type Description
Encoded

The encoded representation of the array.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


encode_cached_obj abstractmethod staticmethod

encode_cached_obj(cache_id: int) -> Encoded

Encode a cached object reference.

Parameters:

Name Type Description Default
cache_id int

The unique identifier for the cached object.

required

Returns:

Type Description
Encoded

The encoded representation of the cached object reference.

Raises:

Type Description
RuntimeError

This is an abstract method that should raise RuntimeError when not implemented by subclasses.


encode_class abstractmethod staticmethod

encode_class(to_encode: type)

Encode a class type into a serializable format.

Parameters:

Name Type Description Default
to_encode type

The class type to be encoded.

required

Returns:

Type Description
Encoded

The encoded representation of the class type.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


encode_dict abstractmethod staticmethod

encode_dict(to_encode: dict, encode_cache: EncodeCache = None, ignore_no_serialize_flags: bool = False) -> Encoded

Encode a dictionary into a serializable format.

Parameters:

Name Type Description Default
to_encode dict

The dictionary to be encoded.

required
encode_cache EncodeCache

Cache used to track encoded objects and avoid duplication.

None
ignore_no_serialize_flags bool

If True, ignores any no-serialize flags on objects. Default is False.

False

Returns:

Type Description
Encoded

The encoded representation of the dictionary.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


encode_function abstractmethod staticmethod

encode_function(to_encode: Callable) -> Encoded

Encode a function into a serializable format.

Parameters:

Name Type Description Default
to_encode Callable

The function to be encoded.

required

Returns:

Type Description
Encoded

The encoded representation of the function.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


encode_iterable abstractmethod staticmethod

encode_iterable(to_encode: EncodableIterables, encode_cache: EncodeCache = None, ignore_no_serialize_flags: bool = False) -> Encoded

Encode an iterable into a serializable format.

Parameters:

Name Type Description Default
to_encode EncodableIterables

The iterable to be encoded.

required
encode_cache EncodeCache

Cache used to track encoded objects and avoid duplication.

None
ignore_no_serialize_flags bool

If True, ignores any no-serialize flags on objects. Default is False.

False

Returns:

Type Description
Encoded

The encoded representation of the iterable.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


encode_primitive abstractmethod staticmethod

encode_primitive(to_encode: EncodablePrimitives)

Encode a primitive value into a serializable format.

Parameters:

Name Type Description Default
to_encode EncodablePrimitives

The primitive value to be encoded.

required

Returns:

Type Description
Encoded

The encoded representation of the primitive value.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.


encode_uncached_obj abstractmethod staticmethod

encode_uncached_obj(to_encode: Serializable, encode_cache: EncodeCache = None, ignore_no_serialize_flags: bool = False)

Encode an object that has not been previously cached.

Parameters:

Name Type Description Default
to_encode Serializable

The object to be encoded.

required
encode_cache EncodeCache

Cache used to track encoded objects and avoid duplication.

None
ignore_no_serialize_flags bool

If True, ignores any no-serialize flags on objects. Default is False.

False

Returns:

Type Description
Encoded

The encoded representation of the object.

Raises:

Type Description
NotImplementedError

This is an abstract method that must be implemented by subclasses.