Skip to content

fix(models): optional fields stay optional with a codec of their own - #105

Merged
kalaspuff merged 1 commit into
mainfrom
fctl/56-https-github-com-kalaspuff-dynamoxy-issu
Oct 30, 2026
Merged

kalaspuff merged 1 commit into
mainfrom
fctl/56-https-github-com-kalaspuff-dynamoxy-issu

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

What and why

A field that chose its codec (codec=, UseCodec) used it as given, so it was never optional and refused None even where its annotation allowed it and None was its default. Attr[dict[str, Any] | None] = field(default=None, codec=JsonCodec()) and Attr[datetime | None] = field(default=None, codec=DateTimeCodec("iso_z")), the guides' way to give one field its own timestamp format, failed on construction with "must not be None". Literal["on", "off", None] failed the same way, since None was one of the literal's values. A pydantic Annotated[datetime | None, UseCodec(...)] = None validated, but create() raised "is required".

A codec given for a type that allows None (X | None, Optional[X], Literal[..., None], also through Annotated and type aliases) is now wrapped in OptionalCodec, as X | None fields without a codec of their own always were: None is not stored and the codec never sees it, a missing attribute reads as None, and remove() and set(None) work. Literal[..., None] resolves to Literal[...] | None. This covers codec=, UseCodec in Attr[Annotated[...]] and pydantic's UseCodec marker; a pydantic TTL() field with a UseCodec had the same problem and is fixed with them.

Verified with unit tests of the codecs and of native and pydantic models, and with integration tests on the in-memory backend and MiniStack that create such items with None, set values, remove them with update() and save(), and read them back. make check passes on Python 3.15, and the affected tests pass on Python 3.12.

Fixes #77

Checklist

  • Tests cover the change (in-memory backend and MiniStack)
  • Docs updated (modeling and pydantic guides, AGENT_GUIDE.md, docstrings, changelog)
  • make check passes locally (lint, types, clippy, consistency, tests)
  • Breaking change? No: these declarations failed before. Only a key field annotated X | None with a codec of its own now fails when its schema is built (below).

Worth confirming

  • Decision: a key field annotated X | None with a codec of its own (sort_key(codec=...)) now raises TypeError ("key attributes cannot be optional") when its schema is built, as the same declaration without a codec always did; it used to be accepted and to refuse None when written.
  • Decision: Literal["on", "off", None] now means Literal["on", "off"] | None everywhere, so such a field can also key a sparse index, which was refused before ("must be stored as text, numbers or bytes").
  • Assumption: Any does not make a field optional: Attr[Any] = field(default=None, codec=JsonCodec()) still refuses None, as Attr[Any] without a codec does.

fctl session


Note

Medium risk. It changes how field codecs are resolved for every model with a codec of its own or a Literal that names None, and a key field annotated X | None with a codec now fails when its schema is built.

What it does. A field that picks its own codec (codec=, UseCodec) used to refuse None even when its annotation allowed it, so declarations like Attr[datetime | None] = field(default=None, codec=DateTimeCodec("iso_z")) failed on construction. A codec given for a type that allows None is now wrapped in OptionalCodec, like fields without a codec of their own, and Literal[..., None] resolves to Literal[...] | None. It covers native models, pydantic models and TTL() fields, with unit and integration tests and updated guides and changelog.

Key fields. A key field annotated X | None with a codec of its own now raises TypeError ("key attributes cannot be optional") when its schema is built, as it already did without a codec; models that declared this and always passed a value stop working.

fctl review's first look at b056240 · 12 files, +251 −17 · Review agent settings

A field that chose its codec (codec=, UseCodec) used the codec as given,
so it was never optional and refused None although its annotation
allowed it: Attr[datetime | None] = field(default=None,
codec=DateTimeCodec("iso_z")) failed on construction with its own
default, and a pydantic Annotated[datetime | None, UseCodec(...)] = None
validated but create() raised "is required". Literal["on", "off", None]
failed the same way, since None was one of the literal's values.

A codec given for a type that allows None (X | None, Optional[X],
Literal[..., None], also through Annotated and type aliases) is now
wrapped in OptionalCodec, as resolve_codec does for X | None: None is
not stored, a missing attribute reads as None, and remove() works.
Literal[..., None] resolves to Literal[...] | None, so it can also key
a sparse index. Optional codecs are never wrapped twice. A key field
annotated X | None now raises "key attributes cannot be optional" with
a codec too, as it did without one.

Fixes #77

Co-authored-by: Carl Oscar Aaro <[email protected]>
@kalaspuff
kalaspuff merged commit 71f7396 into main Oct 30, 2026
22 checks passed
@kalaspuff
kalaspuff deleted the fctl/56-https-github-com-kalaspuff-dynamoxy-issu branch October 30, 2026 23:35
fctl-sh Bot added a commit that referenced this pull request Oct 30, 2026
Brings in #105 (optional fields with a codec of their own). Both sides
appended an entry to the Fixed section of CHANGELOG.md; both are kept,
#105's first since it landed first.

Co-authored-by: Carl Oscar Aaro <[email protected]>
fctl-sh Bot added a commit that referenced this pull request Oct 30, 2026
Keeps both CHANGELOG.md entries under Fixed: the one for optional
fields with a codec of their own (#105), then this branch's for list
elements set past the end.

Co-authored-by: Carl Oscar Aaro <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An explicit codec makes an optional field required

1 participant