Skip to content

fix(models): repositories refuse instances of subclasses and items of other models - #139

Merged
kalaspuff merged 3 commits into
issues-stack/2-stored-keysfrom
issues-stack/3-subclass-instances
Oct 31, 2026
Merged

kalaspuff merged 3 commits into
issues-stack/2-stored-keysfrom
issues-stack/3-subclass-instances

Conversation

@fctl-sh

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

Copy link
Copy Markdown
Contributor

Summary

A repository encoded any instance that isinstance matched with its own schema. client.model(Member).create(Admin(...)) stored _type: "Member" and dropped the subclass's fields, and client.model(Member).get(...) of an Admin item decoded it as a Member, whose save() rewrote the type attribute so that Admin queries no longer found it. A document field typed as a base document dropped a subclass instance's extra fields the same way.

  • Writes through a repository (create, put, save, update, delete, refresh, and their batch and transaction forms with model=) now raise TypeError for instances of a subclass of its model, naming the subclass's repository to use.
  • Reads by key raise ItemDecodeError for items whose type attribute names another model registered for the table. Entity names that no model declares (an entity renamed since) still decode.
  • Fields typed as a native document (or a model used as one) refuse instances of its subclasses with a ValidationError. Pydantic document fields keep pydantic's behaviour, which serializes a subclass instance as the declared class.

Integration tests on the memory backend and MiniStack cover each refusal and check that nothing was written; uv run pytest, ruff, pyrefly and the consistency checks pass. The modeling and single-table guides and the usage guide describe subclasses as models of their own.

Refs #99

Worth confirming

  • Decision: every subclass instance is refused, including subclasses that add only methods and share their base's fields and entity, rather than only those that would lose data. The rule is simpler to predict, and client.model(Subclass) writes them.
  • Decision: an update or delete by key through the base repository is not refused before the write. An update of another model's item (an upsert too) is applied, keeps the item's type attribute, and then raises ItemDecodeError saying it was applied. Upserts now set the type attribute only where the item has none.

fctl session


Note

Medium risk. It changes what repositories accept and return for every model: subclass instances now raise on writes, and reads of items whose type attribute names another model now raise. Code that relied on the old lenient behavior breaks, though only where it was already storing wrong data.

What it does. Repositories used to encode any instance that isinstance matched with their own schema, so client.model(Member).create(Admin(...)) stored the subclass as a Member and dropped its fields, and reading an Admin item through the Member repository decoded it as a Member. Writes through a repository now raise TypeError for subclass instances, reads by key raise ItemDecodeError for items whose type attribute names another registered model, and native document fields refuse subclass instances. Upserts now set the type attribute with if_not_exists, so an upsert through the wrong repository no longer re-types an existing item.

Stricter writes and reads. Every subclass instance is refused by its base model's repository, including subclasses that only add methods, and an update by key through the wrong repository is applied before it raises ItemDecodeError.

fctl review's first look at 32f53f4 · 7 files, +173 −9 · Review agent settings

… other models

A repository encoded any isinstance match with its own schema, so
client.model(Member).create(Admin(...)) stored a Member without the
subclass's fields, and a Member read by key of an Admin item decoded
it, whose save() rewrote the type attribute. Writes now raise TypeError
for instances of subclasses, reads by key raise ItemDecodeError for
items whose type attribute names another model of the table, and fields
typed as native documents refuse instances of the document's subclasses.

Refs #99

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

This comment has been minimized.

Comment thread python/dynamoxy/repository.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 ← this pull request
  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
  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.

1 participant