pycrdt icon indicating copy to clipboard operation
pycrdt copied to clipboard

Add `get_model_state` to get validated doc.

Open mnbbrown opened this issue 2 months ago • 3 comments

Add get_model_state to Doc

This adds get_model_state which returns the entire doc state as the pydantic model pass in as Model= during instantiation.

It's effectively the other side of apply_update.

If the doc has no Model defined it will raise a RuntimeError. If the doc is invalid it will raise a pydantic ValidationError.

Add to_py to Doc get_mode_state uses a new to_py for Doc (1aa8dfca55ea8c08156ae3f867715df9c576397c) It iterates through the roots of the Doc, converting them into their native python types (using the underlying type's to_py() fn).

additional changes Also, update apply_update to use model_validate instead of Model(**value). This is more of a "style" thing. See https://github.com/pydantic/pydantic/discussions/9676

Also add ruff to the test dependencies and format given it's config was already defined in pyproject.toml.

mnbbrown avatar Oct 07 '25 10:10 mnbbrown

@davidbrochart This actually doesn't work in it's current iteration because _roots gets updated lazily when calling get(). Have you had any thoughts about how _roots maybe could be kept in sync? I think either making _roots look at the rust state directly, or updating _roots when running apply_update

Any thoughts?

mnbbrown avatar Oct 07 '25 15:10 mnbbrown

Sorry @mnbbrown I'm a bit low on bandwidth currently, but excited to see what you're doing.

davidbrochart avatar Oct 08 '25 13:10 davidbrochart

Sorry @mnbbrown I'm a bit low on bandwidth currently, but excited to see what you're doing.

No worries! I know how it is :)

I think this is ready for a review now - I hope the description is sufficiently clear. Let me know if anything else needs explanation.

mnbbrown avatar Oct 08 '25 15:10 mnbbrown