Skip to content

Entities

An Entity wraps a dictionary. Declare the fields you want to access as attributes, then pass the dictionary and a Storage to the entity constructor. Attribute writes update the wrapped dictionary. Undeclared keys stay in the data and are included in entity.dict().

Field declarations

An explicit Field or relation descriptor becomes a field. A public annotation such as name: str declares a field, and a public class value such as description = "Project member" supplies a default. Unannotated methods, properties, other callables, and names starting with _ stay out of the implicit field declarations. Annotations describe field types for editors and type checkers. corm uses relation annotations to infer related entity types and whether a field holds one entity or a list.

Input and output keys

origin is the dictionary key a field reads and writes. It defaults to the attribute name. destination is the key used by dict() and defaults to origin. When they differ, the export uses the destination key in place of the origin. Load the export with a model that expects the destination key, or map it back before reloading with the same model.

Access and export

Field and Nested default to AccessMode.ALL. Combine AccessMode flags with |. LOAD processes the field at construction, SET permits assignment, and DUMP exports the field through its declared destination. The GET flag describes a readable field. The current descriptors still allow direct reads without it. KeyNested defaults to GET_SET_LOAD so the original key stays in the exported data. A reverse Relationship lives in the relation index.

dict(strip_none=True) omits declared fields with DUMP whose value is None, including fields in declared nested entities. Undeclared keys with None values are preserved.

The example reads an ID from user_id, exports it as id, and uses a read-only display name. It also shows an optional field and an undeclared key.

from corm import AccessMode, Entity, Field, ReadOnlyFieldError, Storage


class User(Entity):
    id: int = Field(origin="user_id", destination="id")
    name: str = Field(mode=AccessMode.GET | AccessMode.LOAD | AccessMode.DUMP)
    description: str = "Project member"
    nickname: str | None = Field(default=None)


payload = {"user_id": 33, "name": "Amina", "timezone": "UTC", "metadata": None}
user = User(payload, Storage())

assert user.id == 33
user.id = 34
assert payload["user_id"] == 34
assert user.name == "Amina"

try:
    user.name = "Mina"
except ReadOnlyFieldError:
    pass
else:
    raise AssertionError("The name field is read-only")

assert user.dict() == {
    "id": 34,
    "name": "Amina",
    "description": "Project member",
    "nickname": None,
    "timezone": "UTC",
    "metadata": None,
}
assert user.dict(strip_none=True) == {
    "id": 34,
    "name": "Amina",
    "description": "Project member",
    "timezone": "UTC",
    "metadata": None,
}

Default values

Supply a plain class value or pass default= to Field, Nested, or KeyNested. corm calls a default factory for each instance whose input lacks that field. An explicit None value in the input counts as a supplied value.

from corm import Entity, Field, Storage


class Foo(Entity):
    inplace = 1
    in_field = Field(default=2)
    in_field_callable = Field(default=list)


storage = Storage()
foo = Foo(data={}, storage=storage)

assert foo.inplace == 1
assert foo.in_field == 2
assert foo.in_field_callable == []

A callable assigned directly to the class remains a class attribute. Use Field(default=list) if you want corm to call list as a field's default factory.

from corm import Entity, Storage


class Foo(Entity):
    inplace = list


storage = Storage()
foo = Foo(data={}, storage=storage)

assert foo.inplace is list  # still callable, not new empty list