Repository navigation
fix(models)!: None and empty sets read back as written, not as the default - #113
Conversation
…fault
dynamoxy stores None and empty sets as no attribute, and read a
missing attribute as the field's default first. In fields whose
default is neither, writes did not read back: a nickname written as
None with the default "anon" came back as "anon", and a set field
whose default is {"member"}, written empty or emptied by deleting its
last element (DynamoDB removes an emptied set), came back as
{"member"}, so a revoked role was granted again and the next save()
stored it. Pydantic models and documents did the same.
A missing attribute now reads as None in a field that can be None and
as an empty set in a set field; other fields read their default as
before (ADR 0019). Storing NULL instead would break later ADD and
DELETE actions, and DynamoDB refuses NULL in index keys. Items written
before such a field existed read None or an empty set too, which is
the breaking part; a set[X] | None field reads an empty set back as
None.
Updates follow: set(set()) of a model field removes the attribute
like set(None) (it stored a NULL, on which later add() failed),
remove() and set(set()) of a set field empty it (they raised "is
required and cannot be removed"), and == set() / != set() compare
like == None / != None.
Fixes #76
Co-authored-by: Carl Oscar Aaro <[email protected]>
|
fctl reviewed Reads, On lines the diff doesn't show:
Reviewed by fctl · Review agent settings |
…com-kalaspuff-dynamoxy-issu # Conflicts: # docs/guides/modeling.md # python/dynamoxy/AGENT_GUIDE.md
update(Model.key(...), ..., upsert=True) completed every field the
actions leave alone with if_not_exists(field, default). A field that
stores None or an empty set as no attribute cannot tell an existing
item's None from a new item there, so an upsert by key wrote "anon"
over a nickname cleared to None and {"member"} over revoked roles,
which reads now return as written.
Key targets give such fields no default; an item they create reads
None or an empty set there unless the actions set it. Instance
targets still write the instance's values.
Co-authored-by: Carl Oscar Aaro <[email protected]>
|
Upsert by key stores the defaults over cleared values: confirmed on both backends and fixed in 8e0696c.
Instance targets still write the instance's values with |
This comment has been minimized.
This comment has been minimized.
…fault
The previous commit skipped every field that stores a value as no
attribute when an upsert by key completes the item, also set fields
declared without a default. Those never got if_not_exists, and the
check for required fields never saw them, so update(Team.key("t1"),
..., upsert=True) created an item without its required members set
instead of raising ValueError before any I/O. Only fields with a
default are skipped now.
Co-authored-by: Carl Oscar Aaro <[email protected]>
…f plain enum members write (#126) ## What and why Three of the smaller codec issues of #100 still reproduced on main (the fourth, `update(M.tags.set(set()))` and `M.tags.remove()` on a set field, was fixed by #113): - Key templates rendered `Decimal` values of union and `Any` fields as given. With `amount: Attr[Decimal | str]` and `sk="PRICE#{amount}"`, `Decimal("1.50")` wrote `PRICE#1.50`, the instance read back rendered `PRICE#1.5`, and its `save()` raised "key changed". Templates now render a number of such a field as the field reads it back after DynamoDB normalized it, as they already did for `Decimal` fields (ADR 0013): `Decimal("1.50")` as `1.5`, and `2.0` in an `int | float` field as `2`. - `Literal[Color.RED]` with a member of a plain `Enum` passed validation and then failed to write ("cannot store a value of type Color"). The literal codec now stores the member's value (also in key templates and expressions) and reads back the member, as an `Enum` field does. A literal member whose value DynamoDB cannot store (a tuple) is a `TypeError` when the model is declared. - `int` fields raised `OverflowError` for `Decimal("Infinity")` and `decimal.InvalidOperation` for `Decimal("sNaN")`, instead of the validation error other values get. Verified with unit tests of the codecs and of template rendering, which fail without the fix, and integration tests on the in-memory backend and MiniStack: a plain-enum literal in an attribute, a key template, an index key and a filter, and a `Decimal | str` sort key that a loaded instance saves back to the same item. `make check` passes. Fixes #100 ## Checklist - [x] Tests cover the change (in-memory backend and MiniStack) - [x] Docs updated (modeling and single-table guides, changelog) - [x] `make check` passes locally (lint, types, clippy, consistency, tests) - [ ] Breaking change? No: items keyed by a non-canonical number of a union field could not be saved back before; they keep their key and are moved as ADR 0013 describes ## Worth confirming - **Decision:** a `Literal` of plain `Enum` members holds the member in instances (like an `Enum` field) and accepts only the member, not its bare value, when validating; members of `(str, Enum)` mixins stay plain values as before. - **Assumption:** lookups by a non-canonical number of a union field (`get(amount=Decimal("1.50"))`) now address `PRICE#1.5`, so they no longer find items written before with `PRICE#1.50`; such items could not be saved back before either. [fctl session](https://app.fctl.sh/s/ses_01M67V41FM3GB8WN7TS9ZMAKDD) <!-- FCTL_REVIEW_SUMMARY --> --- > [!NOTE] > **Medium risk.** It changes how number values of union and `Any` fields render into key templates and how `Literal` fields of plain `Enum` members are stored, which feeds keys and stored data, though only for fields that did not work or round-trip before. > > **What it does.** Fixes three codec problems from #100. Key templates now render numbers of union and `Any` fields as DynamoDB reads them back (`Decimal("1.50")` as `1.5`), so a loaded item's `save()` no longer reports a changed key. `Literal` fields of plain `Enum` members now store the member's value and read back the member, also inside lists, sets and maps, and `int` fields give a validation error for `Decimal("Infinity")` and `Decimal("sNaN")` instead of crashing. > > **Key rendering change.** Lookups by a non-canonical number of a union field (`get(amount=Decimal("1.50"))`) now address `PRICE#1.5`, so items written earlier under `PRICE#1.50` are no longer found and need the move that ADR 0013 describes. > > <sub>[fctl review](https://app.fctl.sh/s/ses_01M67VS68VEYCWV5RBVWTP0E6Q)'s first look at [`da62fb3`](da62fb3) · 9 files, +222 −18 · [Review agent settings](https://app.fctl.sh/p/dynamoxy?tab=reviews)</sub> <!-- /FCTL_REVIEW_SUMMARY --> --------- Co-authored-by: fctl-sh[bot] <fctl-sh[bot]@users.noreply.github.com> Co-authored-by: Carl Oscar Aaro <[email protected]>
What and why
dynamoxy stores
Noneand empty sets as no attribute, but read a missing attribute as the field's default. So in a field whose default is neither, writes did not read back: anickname: Attr[str | None] = field(default="anon")written asNonecame back as"anon", and arolesset with the default{"member"}, written empty or emptied byUser.roles.delete({"member"}), came back as{"member"}: the revoked role was granted again, and the nextsave()stored it. Pydantic models and documents did the same.A missing attribute now reads as
Nonein a field that can beNoneand as an empty set in a set field, whatever its default; other fields read their default as before (ADR 0019). The stored format does not change: storingNULLinstead was ruled out, sinceADDandDELETEfail on aNULLand index keys refuse one. Aset[X] | Nonefield reads an empty set back asNone, as the guides now say. Updates follow:set(set())of a model field removes the attribute, likeset(None). It stored aNULL, after whichadd()failed.remove()andset(set())of a set field empty it, checked against the field's constraints. They raised "is required and cannot be removed".== set()and!= set()compare like== Noneand!= None, andset_if_not_exists(set())raises likeset_if_not_exists(None).X | Noneand set fields no default.if_not_exists(nickname, "anon")cannot tell an existing item'sNonefrom a new item, so it wrote the default over it; an item such an upsert creates now readsNoneor an empty set there unless the actions set it.Verified with integration tests on the in-memory backend and MiniStack, for native and pydantic models (round trips, items without the attributes, sets emptied in updates and transactions, upserts by key), and unit tests of decoding (documents included) and expressions; they fail without the fix.
make checkpasses, and so does the stage verifier on Python 3.12 and 3.15 with the e2e tests.Breaking change
Items without the attribute of an
X | Nonefield whose default is notNone, or of a set field whose default is not empty (typically items written before the field existed), readNoneor an empty set instead of the default. Where they need the default, backfill it:Or make the field required (
strinstead ofstr | None) when it never needs to beNone. Items that an upsert by key creates readNoneor an empty set in such fields, unless the actions set them (Member.roles.add({"member"}),Member.nickname.set_if_not_exists("anon")).Model.tags == set()now also matches items without the attribute; it matched a storedNULLonly.Worth confirming
== set()and!= set()on a model field compare like== Noneand!= None(no attribute, or aNULL), so that they agree withset(set()), which now stores nothing. Before, they matched a storedNULLonly.if_not_exists, so a constructed instance's value (its default too) fills an existing item'sNoneor empty set. Leaving those fields out, as for key targets, would drop values the caller gave for a created item.dynamo_type("SS","NS"or"BS") behave like set fields: a missing attribute reads as theirdecode(None), not as the field's default. dynamoxy already calleddecode(None)for such fields without a default.Checklist
AGENT_GUIDE.md,skills/dynamoxy, ADR 0019, changelog)make checkpasses locally (lint, types, clippy, consistency, tests)!in the title and explained aboveFixes #76
fctl session
Note
Medium risk. It changes how stored items are decoded and how updates, comparisons and upserts treat
Noneand empty sets, so items written earlier read differently, though the stored format itself does not change.What it does. dynamoxy stores
Noneand empty sets as no attribute but used to read a missing attribute back as the field's default, so a value written asNoneor emptied (a revoked role, say) could come back as the default. A missing attribute now reads asNonein anX | Nonefield and as an empty set in a set field, in native and pydantic models and in documents.set(set()),remove(),== set()andset_if_not_exists(set())follow the same rule, and upserts by key no longer write defaults over an existing item'sNoneor empty set, while still requiring set fields that have no default. ADR 0019, the guides, the changelog and tests on both backends come with it.Breaking read behavior. Items written before a field existed, and items created by an upsert by key, now read
Noneor an empty set instead of a non-empty default inX | Noneand set fields, so existing data may need a backfill withset_if_not_exists.fctl review's first look at
4ee6bfd· 18 files, +429 −57 · Review agent settings