Repository navigation
fix(pydantic): use_enum_values, forward references, RootModel fields and data-taking factories work - #128
Merged
kalaspuff merged 1 commit intoOct 31, 2026
Conversation
…and data-taking factories work Four kinds of pydantic models that pydantic accepts failed in dynamoxy: the enum codec called .value on the plain values that use_enum_values gives; the schema read model_fields before pydantic had rebuilt a model whose annotations name classes declared later; RootModel fields were encoded as documents with a "root" member; and FieldSpec.initial() called default factories that take the validated data without it. Fixes #78 Co-authored-by: Carl Oscar Aaro <[email protected]>
This was referenced Oct 31, 2026
fix(models): numbers of union fields render as stored, and literals of plain enum members write
#126
Merged
Contributor
Author
|
Stack of 10 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.
What and why
Four kinds of pydantic models that pydantic accepts failed in dynamoxy (#78):
use_enum_values=True: instances hold the members' values, and the enum codec called.valueon them, so writes raisedAttributeError: 'str' object has no attribute 'value'. The codec now encodes a member's value as the member, so such models write, read, render key templates and filter like others.shipping: Address | Noneaboveclass Address(BaseModel),create_tables(Order)and writes raisedTypeError: cannot store ... ForwardRef('Address | None'), because dynamoxy readmodel_fieldsbefore pydantic rebuilt the model. dynamoxy now rebuilds an incomplete model (and nested plain models) when it first builds its schema. A name that is still undefined then raises aTypeErrorthat names it.RootModelfields were written as{"root": [...]}and failed to read back. A newRootModelCodecstores a root model as its root value, through the root type's codec (soappend,containsand the other expressions work on it), and reads back the root model.Field(default_factory=lambda data: ...), pydantic 2.10 and later) raised a rawTypeErrorwhen an item without the attribute was read. Reads now leave such defaults to pydantic, which calls the factory with the validated data. Upserts by key call it with the key's fields and the values the update sets.Verified with integration tests on the in-memory backend and MiniStack, each failing without the fix: a
use_enum_valuesmodel with an enum key template, a list of enums, an index query and a filter; a model whose fields name a recursive model declared below it; an undefined forward reference;RootModellist and dict fields withappendandcontains; and a data-taking factory on create, on a read of an item written without the attribute, and on an upsert by key.make checkpasses.Fixes #78
Checklist
make checkpasses locally (lint, types, clippy, consistency, tests)Worth confirming
fctl session
Note
Medium risk. It changes how pydantic models are turned into schemas and how they are encoded and decoded (enum, RootModel and default-factory handling), which every pydantic model in the library goes through, though the fixes only affect models that previously failed.
What it does. Fixes four kinds of pydantic models that failed in dynamoxy: models with
use_enum_values=True, models with annotations that name classes declared later,RootModelfields, anddefault_factoryfunctions that take the validated data. The enum codec now accepts a member's value, a model is rebuilt before its schema is built, a newRootModelCodecstores a root model as its root value, and reads leave data-taking defaults to pydantic while upserts by key call the factory with the key and update values.Stored format of RootModel fields.
RootModelfields are now stored as their root value instead of{"root": ...}; the old form never read back, but the stored shape of such fields changes.fctl review's first look at
63ae135· 8 files, +261 −8 · Review agent settings