|
| 1 | +def merge_params(target, source): |
| 2 | + if isinstance(target, dict) and isinstance(source, dict): |
| 3 | + result = {} |
| 4 | + for k in target: |
| 5 | + if k in source: |
| 6 | + result[k] = merge_params(target[k], source[k]) |
| 7 | + else: |
| 8 | + result[k] = target[k] |
| 9 | + return result |
| 10 | + return source |
| 11 | + |
| 12 | + |
| 13 | +def test_merge_params_full_match(): |
| 14 | + """Source fully matches target structure — all weights replaced.""" |
| 15 | + target = {"a": 1, "b": {"c": 2, "d": 3}} |
| 16 | + source = {"a": 10, "b": {"c": 20, "d": 30}} |
| 17 | + result = merge_params(target, source) |
| 18 | + assert result == {"a": 10, "b": {"c": 20, "d": 30}} |
| 19 | + |
| 20 | + |
| 21 | +def test_merge_params_partial_match(): |
| 22 | + """Target has extra keys not in source — they keep target values.""" |
| 23 | + target = {"a": 1, "b": 2, "new_layer": 99} |
| 24 | + source = {"a": 10, "b": 20} |
| 25 | + result = merge_params(target, source) |
| 26 | + assert result == {"a": 10, "b": 20, "new_layer": 99} |
| 27 | + |
| 28 | + |
| 29 | +def test_merge_params_nested_partial(): |
| 30 | + """Nested dict with partial overlap.""" |
| 31 | + target = {"layer1": {"w": 1}, "layer2": {"w": 2}} |
| 32 | + source = {"layer1": {"w": 10}} |
| 33 | + result = merge_params(target, source) |
| 34 | + assert result == {"layer1": {"w": 10}, "layer2": {"w": 2}} |
| 35 | + |
| 36 | + |
| 37 | +def test_merge_params_source_extra_keys_ignored(): |
| 38 | + """Keys in source but not target are ignored (target structure wins).""" |
| 39 | + target = {"a": 1} |
| 40 | + source = {"a": 10, "extra": 99} |
| 41 | + result = merge_params(target, source) |
| 42 | + assert result == {"a": 10} |
0 commit comments