Skip to content

fix(pydantic): use_enum_values, forward references, RootModel fields and data-taking factories work - #128

Merged
kalaspuff merged 1 commit into
issues-stack/2-client-detailsfrom
issues-stack/3-pydantic-models
Oct 31, 2026
Merged

kalaspuff merged 1 commit into
issues-stack/2-client-detailsfrom
issues-stack/3-pydantic-models

Conversation

@fctl-sh

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

Copy link
Copy Markdown
Contributor

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 .value on them, so writes raised AttributeError: '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.
  • Forward references: with shipping: Address | None above class Address(BaseModel), create_tables(Order) and writes raised TypeError: cannot store ... ForwardRef('Address | None'), because dynamoxy read model_fields before 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 a TypeError that names it.
  • RootModel fields were written as {"root": [...]} and failed to read back. A new RootModelCodec stores a root model as its root value, through the root type's codec (so append, contains and the other expressions work on it), and reads back the root model.
  • Default factories that take the data (Field(default_factory=lambda data: ...), pydantic 2.10 and later) raised a raw TypeError when 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_values model 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; RootModel list and dict fields with append and contains; and a data-taking factory on create, on a read of an item written without the attribute, and on an upsert by key. make check passes.

Fixes #78

Checklist

  • Tests cover the change (in-memory backend and MiniStack)
  • Docs updated (pydantic guide, AGENT_GUIDE, changelog)
  • make check passes locally (lint, types, clippy, consistency, tests)
  • Breaking change? No

Worth confirming

  • Decision: a data-taking factory called by an upsert by key gets the key's fields and the values the update sets, a superset of the earlier fields pydantic would pass. A factory that reads a field the upsert does not know still raises its own error.

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, RootModel fields, and default_factory functions that take the validated data. The enum codec now accepts a member's value, a model is rebuilt before its schema is built, a new RootModelCodec stores 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. RootModel fields 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

…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]>
@fctl-sh

fctl-sh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

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

  1. fix(models): numbers of union fields render as stored, and literals of plain enum members write #126 fix(models): numbers of union fields render as stored, and literals of plain enum members write
  2. fix(client): calls keep their middleware chain and deliver all their events #127 fix(client): calls keep their middleware chain and deliver all their events
  3. fix(pydantic): use_enum_values, forward references, RootModel fields and data-taking factories work #128 fix(pydantic): use_enum_values, forward references, RootModel fields and data-taking factories work ← this pull request
  4. fix(unique)!: upserts that race with creates, unique floats written back, and collections refused #129 fix(unique)!: upserts that race with creates, unique floats written back, and collections refused
  5. fix(transactions): commit() no longer raises after the transaction was applied #130 fix(transactions): commit() no longer raises after the transaction was applied
  6. fix(telemetry): records and metrics count PartiQL writes, throttles of failed calls, batch retries and rows #131 fix(telemetry): records and metrics count PartiQL writes, throttles of failed calls, batch retries and rows
  7. fix(testing): purge_expired, inject_fault(table=) and replayed transactions behave like DynamoDB #132 fix(testing): purge_expired, inject_fault(table=) and replayed transactions behave like DynamoDB
  8. fix(memory): queries refuse empty key values and start keys outside the key condition, and index keys have size limits #133 fix(memory): queries refuse empty key values and start keys outside the key condition, and index keys have size limits
  9. fix(binding): conversions take any mapping and refuse invalid wire values, and unknown table statuses are not active #134 fix(binding): conversions take any mapping and refuse invalid wire values, and unknown table statuses are not active
  10. fix(memory): PartiQL and expressions follow DynamoDB's rules, and hand-written conditions get no redundant parentheses #135 fix(memory): PartiQL and expressions follow DynamoDB's rules, and hand-written conditions get no redundant parentheses

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.

Pydantic models: use_enum_values, forward references, RootModel fields and default factories with data fail

1 participant