Skip to content

refactor: deepen ORM→HTTP serialization behind a Collection seam#54

Merged
lesnik512 merged 1 commit into
mainfrom
refactor/collection-seam-kill-casts
Jun 26, 2026
Merged

refactor: deepen ORM→HTTP serialization behind a Collection seam#54
lesnik512 merged 1 commit into
mainfrom
refactor/collection-seam-kill-casts

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Problem

The persistence→HTTP serialization seam was shallow and leaked. Every list handler assembled its response envelope from raw ORM rows and lied to the type checker:

return typing.cast("schemas.Decks", {"items": objects})

A dict of ORM objects is not a schemas.Decks — the cast just silenced ty. The envelope shape (items) was duplicated at every call site, and the real ORM→Pydantic conversion happened invisibly inside FastAPI's response_model re-validation. Single-object handlers had the same leak in miniature (typing.cast("schemas.Deck", instance)).

Change

Port of modern-python/litestar-sqlalchemy-template#29, adapted to this repo's idiom and extended to converge on the litestar template's end state: no casts anywhere, every handler routed through its schema.

Introduce a deep generic Collection[T] seam in app/schemas.py:

class Collection[T: Base](Base):
    items: list[T]

    @classmethod
    def from_models(cls, objects: Iterable[object]) -> Self:
        return cls.model_validate({"items": list(objects)})

class Cards(Collection[Card]): pass
class Decks(Collection[Deck]): pass

The ORM→schema coercion now happens once, behind a small interface. model_validate (param typed Any) absorbs the coercion honestly, so the leaky casts disappear:

  • Collection handlersschemas.Xs.from_models(objects)
  • Single-object handlersschemas.X.model_validate(instance)

Decks/Cards stay as named subclasses, so OpenAPI schema names are unchanged.

CLAUDE.md's convention is updated to mandate explicit ORM→schema conversion and forbid typing.cast.

Adaptation notes

  • The litestar PR removed four # ty: ignore[invalid-argument-type] suppressions on schemas.Decks(items=...) constructors. This repo had none — instead it used typing.cast(..., {"items": objects}), the local form of the same leak. from_models replaces those.
  • Litestar already used model_validate on single-object handlers, so its PR left them untouched. This repo used casts there, so killing them is what makes this repo converge on the litestar template's shape.
  • The return_dto=PydanticDTO cleanup from the litestar PR has no FastAPI equivalent.

Contract & tests

  • Wire contract unchanged{items: [...]}, same field names, same schema names.
  • ruff format --check, ruff check, ty check all clean.
  • 19 passed, 100% coverage held.

Port of modern-python/litestar-sqlalchemy-template#29, adapted to this
repo's idiom and extended to converge on the litestar template's end
state: no casts anywhere, every handler routed through its schema.

Introduce a deep generic `Collection[T]` seam in app/schemas.py so the
ORM→schema coercion happens once behind a small interface. `Cards`/`Decks`
become `Collection[Card]`/`Collection[Deck]` subclasses (schema names
unchanged). List handlers call `schemas.Xs.from_models(objects)`.

Kill every `typing.cast` in app/api/decks.py — the local equivalent of
the leak the litestar PR removed. Single-object handlers now return via
`schemas.X.model_validate(instance)`; collections via `from_models`.

Update the CLAUDE.md convention to mandate explicit ORM→schema conversion
and forbid casts.

Wire contract unchanged; 19 tests pass, 100% coverage held.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@lesnik512 lesnik512 merged commit a8c9572 into main Jun 26, 2026
2 checks passed
@lesnik512 lesnik512 deleted the refactor/collection-seam-kill-casts branch June 26, 2026 10:13
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