Skip to content

fix(models): documents read from an item keep the keys their class does not declare - #140

Merged
kalaspuff merged 2 commits into
issues-stack/3-subclass-instancesfrom
issues-stack/4-nested-undeclared
Oct 31, 2026
Merged

kalaspuff merged 2 commits into
issues-stack/3-subclass-instancesfrom
issues-stack/4-nested-undeclared

Conversation

@fctl-sh

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

Copy link
Copy Markdown
Contributor

Summary

Writes of loaded instances keep the item's top-level attributes that no field declares (ADR 0014), but a document decoded from a map held its declared fields only. So a save by an older version of an application, during a rolling deploy, erased keys a newer version had added to a document (address.zip_code), although the usage guide says older versions "do not erase what a newer one wrote".

A document read from a stored map (native documents, models used as documents, and pydantic models) now remembers the map keys no field declares, and writes them back whenever it is encoded: save() and put() of its item, update values (Mailbox.history.set([...])), lists and maps of documents, and nested documents. A document the application builds replaces the map, so keys of an old address do not leak into a new one. The keys are kept in the instance's __dict__, outside its fields, so ==, repr() and pydantic's serialization are unchanged. ADR 0020 records the decision.

An integration test on the memory backend and MiniStack stores maps with extra keys the way a newer version would, saves and updates through an older model (native, and a pydantic model with a nested BaseModel), and checks the stored maps. It failed before the change. uv run pytest, ruff, pyrefly and the consistency checks pass.

This completes #99, together with the two pull requests below it in the stack.

Fixes #99

Worth confirming

  • Decision: the remembered keys travel with the document object, not with the item: a loaded document assigned to another item, or copied (copy.copy(), or item.copy() of the model holding it, such as when moving an item to a new key), writes them there too, while the item's undeclared top-level attributes are not copied.

fctl session


Note

Medium risk. It changes what is written back to stored items: every document read from an item now carries and rewrites keys its class does not declare, in save(), put() and update values, for native, model and pydantic documents.

What it does. Documents (nested maps) decoded from a stored item now remember the map keys their class does not declare and write them back when encoded, so an older version of an application no longer erases a key such as address.zip_code that a newer version added. Documents the application builds still replace the map, and the remembered keys sit in the instance's __dict__, outside ==, repr() and pydantic serialization. Copies of a document keep them, as do the documents of item.copy(), but not the item's own undeclared top-level attributes. The change comes with ADR 0020, guide and changelog updates and an integration test that runs on both backends.

Stored-format behaviour. A loaded document now rewrites unknown keys on every save, and a key can only be removed with an update of its path or by assigning a new document (ADR 0020).

fctl review's first look at 2b7bc44 · 7 files, +129 −3 · Review agent settings

…es not declare

Writes of loaded instances kept the item's undeclared top-level
attributes (ADR 0014), but a document decoded from a map held its
declared fields only, so saves by an older version of an application
erased keys a newer one added to documents (address.zip_code). A
document read from a map now remembers the keys no field declares and
writes them back whenever it is encoded; a document the application
builds replaces the map (ADR 0020).

Refs #99

Co-authored-by: Carl Oscar Aaro <[email protected]>
@fctl-review

This comment has been minimized.

Comment thread python/dynamoxy/codecs.py Resolved
@fctl-sh

fctl-sh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Stack of 7 pull requests, merged in this order with "Squash and merge":

  1. fix(memory): PartiQL index reads refuse attributes the index does not hold #137 fix(memory): PartiQL index reads refuse attributes the index does not hold
  2. fix(models): writes through loaded instances use keys stored in another form #138 fix(models): writes through loaded instances use keys stored in another form
  3. fix(models): repositories refuse instances of subclasses and items of other models #139 fix(models): repositories refuse instances of subclasses and items of other models
  4. fix(models): documents read from an item keep the keys their class does not declare #140 fix(models): documents read from an item keep the keys their class does not declare ← this pull request
  5. fix(pydantic): key values and update values run the field's pydantic validators #141 fix(pydantic): key values and update values run the field's pydantic validators
  6. fix(repository): updates render derived attributes from the stored item, and require it unchanged #142 fix(repository): updates render derived attributes from the stored item, and require it unchanged
  7. fix(tables): provisioned billing gives global indexes capacity, and table steps wait for the one before #143 fix(tables): provisioned billing gives global indexes capacity, and table steps wait for the one before

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.

Models: subclass instances, keys stored in non-canonical text, and nested attributes of newer versions

1 participant