Base Classes

BaseField

class pystructs.base.BaseField(default: Any = None, required: bool = True, validators: List[FieldValidator] | None = None)[source]

Bases: ABC

Abstract base class for all fields.

All field types must inherit from this class and implement: - get_size(instance): Return the byte size of the field - parse(buffer, instance): Parse bytes from buffer and return value - serialize(value, instance): Convert value to bytes

Fields also implement the descriptor protocol for attribute access.

abstractmethod get_size(instance: Struct) int[source]

Get the byte size of this field.

Args:

instance: The struct instance (for Ref resolution)

Returns:

Size in bytes

abstractmethod parse(buffer: BinaryIO, instance: Struct) Any[source]

Parse bytes from buffer.

Args:

buffer: Binary stream to read from instance: The struct instance being parsed

Returns:

Parsed value

abstractmethod serialize(value: Any, instance: Struct) bytes[source]

Serialize value to bytes.

Args:

value: Value to serialize instance: The struct instance being serialized

Returns:

Serialized bytes

validate(value: Any, instance: Struct) None[source]

Run field-level validators.

Args:

value: Value to validate instance: The struct instance

Raises:

ValidationError: If validation fails

FixedField

class pystructs.base.FixedField(default: Any = None, required: bool = True, validators: List[FieldValidator] | None = None)[source]

Bases: BaseField

Base class for fixed-size fields.

Subclasses should set the size class attribute to specify the byte size.

get_size(instance: Struct) int[source]

Get the byte size of this field.

Returns:

The fixed size in bytes

size: int = 0