Field types¶
A field can hold most of the types you'd put in a
dataclass. Each one is stored in the segment in a
fixed number of bytes, which is why a value never needs more room than the
field set aside when the box was created. Below, each
type is shown with an example value and the exact bytes a box stores for
it, lowest address first; | separates the parts of a value, and you can
point at a table to read how its types are laid out:
NewType, Final, Annotated and type aliases work too: the box looks
through them to the type they stand for.
What the segment says about a type¶
For a plain int, the kind alone says how the bytes are laid out. For an
enum, a record or a list it doesn't, so the segment also keeps a
description of the type: its members, their
names, a capacity or a shape. That's what lets a C or C++ program read a
field of any type; the segment layout
spells it out.
The schema hash covers these descriptions, so if
an enum gains a member or a record member changes type, the new class no
longer attaches to boxes made with the old one. It doesn't cover choices
that only affect what Python builds when it reads a value back: a record
class's name, list or tuple[T, ...], set or frozenset.
When a read builds a new value¶
A read turns the stored bytes into an object, and whether it builds a new
one each time depends on whether you could change it. A list, a dict, a
set, an array, a TypedDict or a record whose class isn't frozen comes back
as a new object on every read, so changing the one you hold changes nothing
in the box; to store the change, assign it back. A value you can't change,
such as a datetime, a Decimal, a UUID or an enum member, is built
again only after a write to its field, and until then every read returns
the same object, so box.when is box.when holds. A tuple, a frozenset or a
record whose class is declared frozen counts only when everything it holds
can't be changed either, so a tuple holding a list is built on every read.
A record is built by calling its class, which means its __post_init__,
converters and validators run on every read when the record's class isn't
frozen, and only on the first read after each write when it is.
Dates and times¶
A datetime or time is either naive or keeps a fixed offset from UTC.
If yours has a time zone, the box stores its offset at that moment, in
whole minutes, and gives it back with a datetime.timezone of that offset,
so a named zone such as a ZoneInfo comes back as a plain offset. A time
needs a tzinfo that can give an offset without a date. A date field
refuses a datetime, since storing it would lose the time of day.
Enums and literals¶
An enum member is stored by its position in the class, so reading gives
you back the member itself and is comparisons work. Reordering the
members changes what the stored positions mean, so the schema hash changes
with it and old boxes won't attach. In a Literal field, values of
different types count as different values even when Python would call them
equal: in Literal[1, True], 1 and True are two separate values, and
each reads back as itself.
Unions¶
A union stores which of its members it holds, so a value comes back as the
type you wrote. A value whose type is exactly one member's type goes to that
member; otherwise the most specific member that takes it wins, so bool
comes before int, and float | int keeps 3 an int. Two members that
take values of the same class, such as list[int] | list[str], are refused
with TypeError when you define the class, because the box couldn't tell
which one a stored value belongs to.
A numpy number whose class is no member's class goes to the first member
of its own kind of number, so numpy.int64(5) written to float | int is
stored through the int member and reads back as 5. If that member can't
hold the value, you get an OverflowError; the box doesn't try the next
member.
numpy numbers¶
An int, float, complex or bool field also takes numpy's scalars, and
reads back the type it declares, so numpy.uint16(7) written to an int field
reads back as the int 7. A field can declare a numpy type too: the
fixed-width integers from int8 to uint64, float16, float32, complex64
and datetime64 each take the bytes of their width, and read back as that
numpy type. numpy.int64, numpy.float64, numpy.complex128 and
numpy.bool_ store the same bytes as int, float, complex and bool, so
two classes with the same identity, one declaring int
and the other numpy.int64, attach to the same box, and each reads its own
type. How to store numpy numbers shows each
of them.
When a field counts as changed¶
events and
watch compare stored bytes, not Python
values, to decide whether a field changed. That gives a few surprises:
writing NaN again reports nothing, while -0.0 after 0.0 reports a
change, and so does True after 1 in an int | bool field.
Large values¶
A box has one write lock, so while a large array is copied in or out, every other read and write of that box waits. The copy itself runs with the GIL released, so your other threads keep going. Keep a large array in a box of its own, so small fields don't wait behind it. How fast a box is gives the cost of the copy, and How to store arrays shows how to read into an array you already have.