Skip to content

fix(models): members of mixin enums are the plain values of str and int fields - #69

Merged
kalaspuff merged 3 commits into
fixes-stack/07-unversioned-itemsfrom
fixes-stack/08-enum-values
Oct 30, 2026
Merged

kalaspuff merged 3 commits into
fixes-stack/07-unversioned-itemsfrom
fixes-stack/08-enum-values

Conversation

@fctl-sh

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

Copy link
Copy Markdown
Contributor

What and why

A member of a (str, Enum) or (int, Enum) passed the validation of a str, int, Literal or union (str | int) field and stayed an enum member. These are the mixins of code written before enum.StrEnum and IntEnum. DynamoDB stored its value ("book"), but key templates, derived index keys and unique guards rendered it with str(), and for such enums that gives "Kind.BOOK". With pk="KIND#{kind}#{sku}" and Product(kind=Kind.BOOK, sku="s1", ...):

  • The item was stored under KIND#Kind.BOOK#s1 while its kind attribute held "book", so get(kind="book", sku="s1") returned None.
  • The instance read back renders KIND#book#s1, so its save() raised ValueError (the key changed since it was loaded).
  • A derived index key became CODE#Code.ONE, and the unique guard of label=Kind.BOOK was ..._unique#Product#label#Kind.BOOK, so a second item with label="book" was accepted.

StrEnum, IntEnum and pydantic models were not affected. The str, int and union codecs now return the plain str or int a subclass holds (str.__str__, int.__int__), and the Literal codec returns the plain value of the literal a value matches, also for literals that are members of such an enum (Literal[Kind.BOOK, Kind.CD]). So instances, templates, guards and conditions all use what DynamoDB stores. Field paths and key lookups validate before encoding, which covers update actions, conditions and reads by key too.

Verified with a new integration test on the in-memory backend and MiniStack (the key, an index key with a Literal field, a Literal of enum members and a union field, the unique guard, a read by value and save() of the item read back), and with the whole suite, ruff, pyrefly and the consistency checks.

Checklist

  • Tests cover the change (in-memory backend and MiniStack)
  • Docs updated (modeling guide, changelog)
  • make check passes locally (lint, types, consistency, tests; no Rust change)
  • Breaking change? No; items already stored under such keys keep them

Worth confirming

  • Decision: a str or int field now holds the plain value even when given a StrEnum or IntEnum member (user.name = Kind.BOOK reads back as "book"), as an item read back always did; fields typed with the enum keep their members.
  • Assumption: items already written with such keys (KIND#Kind.BOOK#s1) are not migrated: nothing uses the library yet, so this pull request leaves them out.

fctl session


Note

Medium risk. It changes what validate returns for str, int, Literal and str | int fields, which feeds the keys, templates and unique guards of every model, though only when a value is a subclass of str or int such as a mixin enum member.

What it does. Members of (str, Enum) and (int, Enum) mixin enums used to be stored as their value but rendered as Kind.BOOK in keys, templates and unique guards, so items got keys that reads by the value missed and save() of a loaded item failed. The str, int, Literal and union codecs now turn such a value into the plain str or int it holds, so what a model holds, renders and stores agree. A test covers creation, a read, a save and an index query, and the changelog and modeling guide are updated.

Stored keys. Items that were already written with the Kind.BOOK form of a key before this fix are not migrated, and reads by the plain value still miss them.

fctl review's first look at c13b53f · 4 files, +89 −10 · Review agent settings

@fctl-review

This comment has been minimized.

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

fctl-sh Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Stack of 5 open pull requests (from a pass over the library for bugs; #61 to #65 are merged already), merged in this order with "Squash and merge":

  1. fix(client): writes whose retries run out after an uncertain attempt are unknown #67 fix(client): writes whose retries run out after an uncertain attempt are unknown
  2. fix(repository): items stored before the model had a version can be saved #68 fix(repository): items stored before the model had a version can be saved
  3. fix(models): members of mixin enums are the plain values of str and int fields #69 fix(models): members of mixin enums are the plain values of str and int fields ← this pull request
  4. fix(query): requests of growing reads ask for at most a page's worth of items #70 fix(query): requests of growing reads ask for at most a page's worth of items
  5. fix(errors): errors pickle, so they cross process boundaries #72 fix(errors): errors pickle, so they cross process boundaries

The findings that are not fixed here are filed as issues (see the next comment on #72).

@fctl-review

This comment has been minimized.

Comment thread python/dynamoxy/codecs.py Resolved
fctl-sh Bot and others added 2 commits October 30, 2026 22:40
…nt fields

A member of a `(str, Enum)` or `(int, Enum)`, the mixins of code
written before StrEnum and IntEnum, passed the validation of a str, int
or Literal field and was kept as it was. The item stored its value
(`"book"`), but key templates, derived index keys and unique guards
rendered its str(), which is "Kind.BOOK" for such enums. The item got
the key `KIND#Kind.BOOK#s1`: reads by the value missed it, the loaded
instance rendered `KIND#book#s1` so its save() raised "key changed",
and a unique value written as "book" was not taken.

The str and int codecs now return the plain str or int a subclass holds,
and the Literal codec the literal a value matches, so instances,
templates, guards and conditions use what DynamoDB stores.

Co-authored-by: Carl Oscar Aaro <[email protected]>
…s too

`Literal[Kind.BOOK, Kind.CD]` with `class Kind(str, Enum)`: the Literal
codec returned the declared member, whose str() is "Kind.BOOK", so
templates and guards rendered it again, now also for items read back.
It returns the plain str or int of the literal a value matches.

Co-authored-by: Carl Oscar Aaro <[email protected]>
@kalaspuff
kalaspuff force-pushed the fixes-stack/08-enum-values branch from 89ee47a to df4871a Compare October 30, 2026 21:40
… too

`Attr[str | int]` (the union the usage guide suggests for changing a
field's type) kept a `(str, Enum)` member as it was, so templates
rendered `K#Kind.BOOK`. The union codec returns the plain value as well.

Co-authored-by: Carl Oscar Aaro <[email protected]>
@kalaspuff
kalaspuff merged commit 2151150 into main Oct 30, 2026
23 checks passed
@kalaspuff
kalaspuff deleted the fixes-stack/08-enum-values branch October 30, 2026 22:08
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