Repository navigation
fix(models): repositories refuse instances of subclasses and items of other models - #139
Merged
kalaspuff merged 3 commits intoOct 31, 2026
Conversation
… 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]>
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Carl Oscar Aaro <[email protected]>
Co-authored-by: Carl Oscar Aaro <[email protected]>
Contributor
Author
|
Stack of 7 pull requests, merged in this order with "Squash and merge":
|
This was referenced Oct 31, 2026
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.
Summary
A repository encoded any instance that
isinstancematched with its own schema.client.model(Member).create(Admin(...))stored_type: "Member"and dropped the subclass's fields, andclient.model(Member).get(...)of anAdminitem decoded it as aMember, whosesave()rewrote the type attribute so thatAdminqueries no longer found it. A document field typed as a base document dropped a subclass instance's extra fields the same way.model=) now raiseTypeErrorfor instances of a subclass of its model, naming the subclass's repository to use.ItemDecodeErrorfor items whose type attribute names another model registered for the table. Entity names that no model declares (an entity renamed since) still decode.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
client.model(Subclass)writes them.ItemDecodeErrorsaying 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
isinstancematched with their own schema, soclient.model(Member).create(Admin(...))stored the subclass as aMemberand dropped its fields, and reading anAdminitem through theMemberrepository decoded it as aMember. Writes through a repository now raiseTypeErrorfor subclass instances, reads by key raiseItemDecodeErrorfor items whose type attribute names another registered model, and native document fields refuse subclass instances. Upserts now set the type attribute withif_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