Skip to content

feat(bfabric): record entity provenance in dump_yaml / load_yaml - #608

Draft
leoschwarz wants to merge 2 commits into
mainfrom
feature/entity-serialization-metadata
Draft

feat(bfabric): record entity provenance in dump_yaml / load_yaml#608
leoschwarz wants to merge 2 commits into
mainfrom
feature/entity-serialization-metadata

Conversation

@leoschwarz

Copy link
Copy Markdown
Member
  • Change Entity.dump_yaml to record the entity's URI, the dump time and the bfabricPy version alongside the data, so Entity.load_yaml restores a working uri and refs without being told the B-Fabric instance.
  • Change Entity.load_yaml to return the most specific entity class for the dumped type, and raise TypeError when called on a subclass that does not match.
  • Change dump_yaml to require the entity to have a bfabric_instance. Files written by earlier versions still load, with a DeprecationWarning.
  • Reject a loaded file whose id/classname disagree with its recorded URI, instead of silently producing an entity that points somewhere else.

Closes #351

🤖 Prepared with assistance from Claude Opus 5 via Claude Code.

@github-actions

Copy link
Copy Markdown
Contributor

📝 "TODO" Changes Detected

Summary: ✅ 1 "TODO" removed

✅ Removed "TODO"s (1)


This comment is automatically updated when "TODO" changes are detected.

A dumped entity was a bare data dictionary, which lost the instance it came
from. Loading it back without passing bfabric_instance produced a half-broken
object: both .uri and .refs call EntityUri.from_components(None, ...) and
raise. A dump of workunit 1234 on the test instance was also indistinguishable
from one on production, since ids are only unique within an instance.

Wrap the data in a versioned envelope carrying the entity URI, the dump time
and the bfabricPy version. The URI is the only provenance field needed, as it
encodes instance, entity type and id, and it is already validated. A model
validator cross-checks it against the data's id and classname, so a mismatched
or hand-edited file fails at load instead of silently producing a wrong entity.

load_yaml now dispatches to the most specific class for the dumped type, and
rejects a file holding a different type when called on a subclass. Files
written by earlier versions still load, with a DeprecationWarning.

The data dictionary stays unvalidated: it is an opaque API payload, and pydantic
cannot resolve ApiResponseObjectType anyway (an implicit recursive alias, which
hits a RecursionError), so validating it would risk coercing the payload.

Closes #351
Collapse the load_yaml class-dispatch ladder, trim the docstrings to the
contract the signatures cannot show, and drop a redundant local rebind in
parse_document. Bind the parsed YAML document to an object-annotated local
so basedpyright does not see an Any argument, and drop the two baseline
entries the rewrite made obsolete. No behaviour change.
@leoschwarz
leoschwarz force-pushed the feature/entity-serialization-metadata branch from 11fa791 to 2313568 Compare August 21, 2026 12:44
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.

Improve entity serialization

1 participant