Skip to content

How to store records

When several values belong together, such as an x and a y, you can keep them in one field as a record: a dataclass, a NamedTuple, a fixed-length tuple, a TypedDict, an attrs class or a msgspec.Struct. Each member can have any type a field could have on its own, so a text member needs its capacity too.

1. Declare the record and the field

Define the record class as usual, then use it as a field's type:

from dataclasses import dataclass
from typing import Annotated, NamedTuple, NotRequired, TypedDict

from sharedbox import Capacity, SharedBox, field


@dataclass(frozen=True)
class Address:
    street: Annotated[str, Capacity(32)]
    number: int
    city: Annotated[str, Capacity(32)]


class Hours(NamedTuple):
    opens: int
    closes: int


class Instructions(TypedDict):
    attempts: int
    note: NotRequired[Annotated[str, Capacity(16)]]


class Recipient(SharedBox, name="example-recipient"):
    address: Address = Address("", 0, "")
    hours: Hours = Hours(8, 18)
    instructions: Instructions = field(default_factory=lambda: {"attempts": 1})

2. Write and read the whole record

When you assign a record, every member is written at once, so a reader sees all of the new values or none of them. Reading gives you an instance of the class, a new one on every read unless the class is frozen and every member holds a value that can't be changed (see When a read builds a new value):

recipient = Recipient()
recipient.address = Address("High Street", 12, "Leeds")
print(recipient.address)  # Address(street='High Street', number=12, city='Leeds')

Your constructor runs when a read builds the record

A read builds the record by calling its class, so __post_init__, attrs 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. Make sure running them a second time changes nothing: a converter that rounds a float, for example, must leave an already rounded float as it is, or a read won't match what you wrote.

3. Change one member

Changing a record you read from the box changes nothing in the box. To change one member, read the record, make a new one with the change, and assign it back:

recipient.hours = recipient.hours._replace(closes=17)
print(recipient.hours)  # Hours(opens=8, closes=17)

If you use a frozen dataclass or a NamedTuple, the record you read is read-only, so forgetting to assign it back gives you an error instead of a silent no-op.

4. Leave out a TypedDict key

A key marked NotRequired can be left out, and it is left out again when you read the record back:

print(recipient.instructions)  # {'attempts': 1}
recipient.instructions = {"attempts": 2, "note": "leave at door"}
print(recipient.instructions["note"])  # leave at door

Assigning a mapping with a key the TypedDict doesn't declare, or without a required key, raises TypeError.

What a record cannot hold

A read has to rebuild the record from its stored members, so a record that can't be rebuilt that way is refused with TypeError as soon as you define the box class. That covers a dataclass or attrs field with init=False, an InitVar without a default, and a generic record class. A record also can't hold a reference field.

The whole script
"""The script of the guide "How to store records"."""

from dataclasses import dataclass
from typing import Annotated, NamedTuple, NotRequired, TypedDict

from sharedbox import Capacity, SharedBox, field


@dataclass(frozen=True)
class Address:
    street: Annotated[str, Capacity(32)]
    number: int
    city: Annotated[str, Capacity(32)]


class Hours(NamedTuple):
    opens: int
    closes: int


class Instructions(TypedDict):
    attempts: int
    note: NotRequired[Annotated[str, Capacity(16)]]


class Recipient(SharedBox, name="example-recipient"):
    address: Address = Address("", 0, "")
    hours: Hours = Hours(8, 18)
    instructions: Instructions = field(default_factory=lambda: {"attempts": 1})



recipient = Recipient()
recipient.address = Address("High Street", 12, "Leeds")
print(recipient.address)  # Address(street='High Street', number=12, city='Leeds')

recipient.hours = recipient.hours._replace(closes=17)
print(recipient.hours)  # Hours(opens=8, closes=17)

print(recipient.instructions)  # {'attempts': 1}
recipient.instructions = {"attempts": 2, "note": "leave at door"}
print(recipient.instructions["note"])  # leave at door

recipient.close()
Recipient.unlink()