Skip to content

Standardise YAML serialization behind a shared Protocol and generic helper#405

Merged
tschm merged 2 commits intomainfrom
copilot/standardise-yaml-serialization
Mar 8, 2026
Merged

Standardise YAML serialization behind a shared Protocol and generic helper#405
tschm merged 2 commits intomainfrom
copilot/standardise-yaml-serialization

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 8, 2026

TemplateLock, RhizaTemplate, and RhizaBundles each duplicated from_yaml/to_yaml with no shared contract, making generic utilities and type-safe dispatch impossible.

Changes

  • src/rhiza/models/_base.py (new) — introduces two public symbols:

    • YamlSerializable@runtime_checkable Protocol declaring from_yaml(Path) -> Self and to_yaml(Path) -> None; all three models satisfy it structurally with no inheritance changes required
    • load_model(cls, path) — generic typed helper preserving the concrete return type via TypeVar, with an explicit TypeError guard for non-conforming classes
  • bundle.py — adds RhizaBundles.to_yaml so all three models fully satisfy YamlSerializable

  • models/__init__.py — re-exports YamlSerializable and load_model

Usage

from rhiza.models._base import load_model, YamlSerializable
from rhiza.models.lock import TemplateLock
from rhiza.models.template import RhizaTemplate

# Generic load — return type is inferred correctly by type checkers
lock = load_model(TemplateLock, Path(".rhiza/template.lock"))   # TemplateLock
tmpl = load_model(RhizaTemplate, Path(".rhiza/template.yml"))  # RhizaTemplate

# Runtime protocol check
assert isinstance(lock, YamlSerializable)
Original prompt

This section details on the original issue you should resolve

<issue_title>yaml</issue_title>
<issue_description>10. 📝 Standardise to_yaml / from_yaml behind a common base class or Protocol

Files: src/rhiza/models/lock.py, src/rhiza/models/template.py, src/rhiza/models/bundle.py

All three models (TemplateLock, RhizaTemplate, RhizaBundles) implement from_yaml / to_yaml independently with no shared interface. Introduce a YamlSerializable Protocol (or abstract base YamlModel) in src/rhiza/models/_base.py to enforce the contract, improve type-checking, and allow generic utilities (e.g., a load_model(cls, path) helper).</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…les.to_yaml

Co-authored-by: tschm <2046079+tschm@users.noreply.github.com>
@tschm tschm marked this pull request as ready for review March 8, 2026 09:37
Copilot AI changed the title [WIP] Standardise to_yaml / from_yaml behind a common base class Standardise YAML serialization behind a shared Protocol and generic helper Mar 8, 2026
Copilot finished work on behalf of tschm March 8, 2026 09:37
@tschm tschm merged commit 0f4313f into main Mar 8, 2026
15 checks passed
@tschm tschm deleted the copilot/standardise-yaml-serialization branch March 8, 2026 09:38
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.

yaml

2 participants