Merger API overwrites LHS data
leviem1 opened this issue · 2 comments
Operating System
- Name/Distribution: macOS
- Version: Sonoma 14.0
Version of Python and packages in use at the time of the issue.
- Distribution: CPython
- Python Version: 3.12.0
- Version of yamlpath installed: 3.8.1
- Version of ruamel.yaml installed: 0.17.21
Minimum sample of YAML (or compatible) data necessary to trigger the issue
lhs_data:
a: 1
rhs_data:
b: 2
Complete steps to reproduce the issue when triggered via Library:
lhs_data = ... # Load from Parsers.get_yaml_data()
rhs_data = ... # Load from Parsers.get_yaml_data()
log = ConsolePrinter(SimpleNamespace(quiet=True, verbose=True, debug=True))
merger = Merger(
log,
lhs_data,
MergerConfig(
logger=log,
args=argparse.Namespace(config=tf.name),
),
)
merger.merge_with(rhs_data)
merger.prepare_for_dump(Parsers.get_yaml_editor(), "out.yaml")
output = merger.data
Expected Outcome
Library should not mutate LHS data.
lhs == {"a": 1}
rhs == {"b": 2}
output == {"a": 1, "b": 2}
Actual Outcome
Library mutates LHS data.
lhs == {"a": 1, "b": 2}
rhs == {"b": 2}
output == {"a": 1, "b": 2}
Screenshot(s), if Available
N/a
I was able to work around this issue in my code by creating the following function:
def copy_data(data: Any, yaml: YAML) -> Any:
buf = io.BytesIO()
yaml.dump(data, buf)
buf.flush()
buf.seek(0)
return yaml.load(buf)
Which I've used as follows:
merger = Merger(
log,
copy_data(lhs_data, yaml),
MergerConfig(
logger=log,
args=argparse.Namespace(config=tf.name),
),
)
I'd still prefer that my lhs_data
not be mutated, especially because this make the Merger.data
attribute redundant IMO, but I understand implementing a change like this would cause breaking changes due to the requirement of the YAML instance. I don't think there's a simple solution for this.
Nonetheless, I think my workaround might be helpful for new users encountering this behavior for the first time.
The described behavior is as designed and covered in the documentation. When you need to preserve a copy of the LHS document, it is up to you to do so before wrapping the document with the Merger
.