Commit d798a8a
fix(dpmodel): preserve model dictionaries during serialization (#5788)
## Summary
- preserve the caller-owned model dictionary while save_dp_model()
rewrites variables into YAML or HDF5 references;
- copy only the nested dict and list containers that traversal mutates,
so large arrays are not duplicated;
- retain support for non-copyable variable objects such as h5py.Dataset;
- add regressions for nested YAML/native serialization and an
HDF5-backed variable.
## Why existing tests missed this
The existing save/load tests pass deepcopy(self.model_dict) into
save_dp_model() and then validate only the serialized round trip. That
disposable copy hides mutations of the object supplied by a real caller,
so the shallow-copy bug was never observed.
The new tests pass a caller-owned nested structure directly, assert that
its containers and variables remain unchanged, and cover both
serialization implementations. The HDF5 dataset case also prevents a
memory-heavy full deepcopy from becoming the fix.
## Validation
- targeted serialization tests and existing save/load tests: 6 passed;
- ruff format .;
- ruff check .;
- git diff --check.
Closes #5639.
Coding agent: Codex
Codex version: codex-cli 0.144.1
Model: gpt-5.6-sol
Reasoning effort: xhigh
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Saving models no longer alters the original nested data structures.
* Preserves shared and non-copyable objects, including HDF5-backed
datasets, during serialization.
* Ensures saved models retain expected data when loaded across supported
formats.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Co-authored-by: njzjz-bot <njzjz-bot@users.noreply.github.com>1 parent 1c5ff68 commit d798a8a
2 files changed
Lines changed: 93 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
66 | 82 | | |
67 | 83 | | |
68 | 84 | | |
| |||
91 | 107 | | |
92 | 108 | | |
93 | 109 | | |
94 | | - | |
| 110 | + | |
| 111 | + | |
95 | 112 | | |
96 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
97 | 116 | | |
98 | 117 | | |
99 | 118 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
0 commit comments