Repository navigation
fix(models): optional fields stay optional with a codec of their own - #105
Merged
Merged
Conversation
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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
A field that chose its codec (
codec=,UseCodec) used it as given, so it was never optional and refusedNoneeven where its annotation allowed it andNonewas its default.Attr[dict[str, Any] | None] = field(default=None, codec=JsonCodec())andAttr[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, sinceNonewas one of the literal's values. A pydanticAnnotated[datetime | None, UseCodec(...)] = Nonevalidated, butcreate()raised "is required".A codec given for a type that allows
None(X | None,Optional[X],Literal[..., None], also throughAnnotatedand type aliases) is now wrapped inOptionalCodec, asX | Nonefields without a codec of their own always were:Noneis not stored and the codec never sees it, a missing attribute reads asNone, andremove()andset(None)work.Literal[..., None]resolves toLiteral[...] | None. This coverscodec=,UseCodecinAttr[Annotated[...]]and pydantic'sUseCodecmarker; a pydanticTTL()field with aUseCodechad 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 withupdate()andsave(), and read them back.make checkpasses on Python 3.15, and the affected tests pass on Python 3.12.Fixes #77
Checklist
AGENT_GUIDE.md, docstrings, changelog)make checkpasses locally (lint, types, clippy, consistency, tests)X | Nonewith a codec of its own now fails when its schema is built (below).Worth confirming
X | Nonewith a codec of its own (sort_key(codec=...)) now raisesTypeError("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 refuseNonewhen written.Literal["on", "off", None]now meansLiteral["on", "off"] | Noneeverywhere, so such a field can also key a sparse index, which was refused before ("must be stored as text, numbers or bytes").Anydoes not make a field optional:Attr[Any] = field(default=None, codec=JsonCodec())still refusesNone, asAttr[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
Literalthat namesNone, and a key field annotatedX | Nonewith a codec now fails when its schema is built.What it does. A field that picks its own codec (
codec=,UseCodec) used to refuseNoneeven when its annotation allowed it, so declarations likeAttr[datetime | None] = field(default=None, codec=DateTimeCodec("iso_z"))failed on construction. A codec given for a type that allowsNoneis now wrapped inOptionalCodec, like fields without a codec of their own, andLiteral[..., None]resolves toLiteral[...] | None. It covers native models, pydantic models andTTL()fields, with unit and integration tests and updated guides and changelog.Key fields. A key field annotated
X | Nonewith a codec of its own now raisesTypeError("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