refactor: deepen ORM→HTTP serialization behind a Collection seam#54
Merged
Conversation
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>
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.
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:
A
dictof ORM objects is not aschemas.Decks— thecastjust silencedty. The envelope shape (items) was duplicated at every call site, and the real ORM→Pydantic conversion happened invisibly inside FastAPI'sresponse_modelre-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 inapp/schemas.py:The ORM→schema coercion now happens once, behind a small interface.
model_validate(param typedAny) absorbs the coercion honestly, so the leaky casts disappear:schemas.Xs.from_models(objects)schemas.X.model_validate(instance)Decks/Cardsstay as named subclasses, so OpenAPI schema names are unchanged.CLAUDE.md's convention is updated to mandate explicit ORM→schema conversion and forbidtyping.cast.Adaptation notes
# ty: ignore[invalid-argument-type]suppressions onschemas.Decks(items=...)constructors. This repo had none — instead it usedtyping.cast(..., {"items": objects}), the local form of the same leak.from_modelsreplaces those.model_validateon 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.return_dto=PydanticDTOcleanup from the litestar PR has no FastAPI equivalent.Contract & tests
{items: [...]}, same field names, same schema names.ruff format --check,ruff check,ty checkall clean.