Skip to Content
Binal
API Reference

Schema and Layout

Static schema introspection and runtime layout

schema() and layout() answer different questions. The schema is static: a model’s shape, field offsets, and fixed size, where they are known without an instance. The layout is runtime: where each field of one particular instance actually lands when encoded.

Schema / FieldInfo

Model.schema() returns a Schema with model_name, byte_order, bit_order, fields (a list of FieldInfo), and fixed_size (the total size in bytes for fixed-size models, or None for dynamic models). Schema.field(name) looks a field up by name and raises KeyError for an unknown name. Each FieldInfo carries name, type_name, offset (the byte offset, or None when it is not statically known), size (in bytes, or None), and metadata (the field’s Annotated markers).

from typing import Annotated

from binal import BinaryModel, Prefixed, UInt8, UInt16


class Packet(BinaryModel, byte_order="big"):
    version: UInt8
    payload: Annotated[bytes, Prefixed(UInt16)]


schema = Packet.schema()
schema.fields
schema.fixed_size
schema.field("version").offset
schema.field("version").size
Packet(version=1, payload=b"hi").encoded_size()

fixed_size() returns the total size in bytes for fixed-size models, or None for dynamic models. Field offsets are available when all preceding fields have a known fixed size.

ModelLayout / FieldLayout

instance.layout() returns a ModelLayout describing this instance’s actual encoding: fields (a tuple of FieldLayout), total_bytes, and total_bits. Each FieldLayout has name, path, kind, byte_offset, bit_offset, byte_size, bit_size, and children. Unlike schema(), the layout reflects the instance’s actual dynamic sizes, and bit fields report the byte they share with their neighbours. Nested models get children from their own layout, shifted to their position; lists of models get one child layout per element. Fields skipped by a When condition do not appear.

from typing import Annotated

from binal import BinaryModel, LengthOf, Prefixed, Sized, UInt16


class Packet(BinaryModel, byte_order="big"):
    length: Annotated[UInt16, LengthOf("payload")]
    payload: Annotated[bytes, Sized("length")]


layout = Packet(payload=b"Hello").layout()
layout.total_bytes  # 7
layout.total_bits  # 56
layout.fields[0].name  # "length"
layout.fields[0].byte_size  # 2
layout.fields[1].name  # "payload"
layout.fields[1].byte_offset  # 2
layout.fields[1].byte_size  # 5

__binal_validate__

A model may define an instance method __binal_validate__(self) to check invariants spanning several fields. It runs after construction and after decode, and raises ValidationError when the instance is invalid. Other exceptions raised by the hook are wrapped in ValidationError.

from binal import BinaryModel, UInt8, ValidationError


class Range(BinaryModel, byte_order="big"):
    lo: UInt8
    hi: UInt8

    def __binal_validate__(self) -> None:
        if self.lo > self.hi:
            raise ValidationError("lo must be <= hi", path="Range")


Range(lo=5, hi=2)  # ValidationError, at construction
Range.decode(b"\x05\x02")  # ValidationError, at decode