From 493adc87df789aa058da84a4d8c009d9cc08591a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Thu, 19 Nov 2026 10:05:02 +0100 Subject: [PATCH] fix: fcs and tsv export modified the `meta_data` dictionary of the caller Co-Authored-By: Claude Opus 5.5 --- CHANGELOG | 2 ++ dclab/rtdc_dataset/export.py | 8 ++++---- tests/test_rtdc_export_fcs.py | 11 +++++++++++ tests/test_rtdc_export_tsv.py | 10 ++++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index c6352a6f..50e63cbf 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -264,6 +264,8 @@ - fix: `RTDC_Dict` did not allow "file" basins, because the class attribute `_local_basins_allowed` was shadowed by the instance attribute set in `RTDCBase.__init__` (#421) + - fix: `Export.fcs` and `Export.tsv` added the "dclab version" key to + the `meta_data` dictionary passed by the caller (#422) 0.74.0 - fix: CLI commands compress, condense, join, and repack deleted the input file if it was also given as the output path diff --git a/dclab/rtdc_dataset/export.py b/dclab/rtdc_dataset/export.py index eca5206e..f1c7d6ba 100644 --- a/dclab/rtdc_dataset/export.py +++ b/dclab/rtdc_dataset/export.py @@ -187,8 +187,8 @@ def fcs(self, Due to incompatibility with the .fcs file format, events that have a NaN value in any of the exported features are not exported. """ - if meta_data is None: - meta_data = {} + # copy to not modify the dictionary passed by the caller + meta_data = dict(meta_data) if meta_data is not None else {} if not FCSWRITE_AVAILABLE: raise ModuleNotFoundError( "Package `fcswrite` required for fcs export!") @@ -669,8 +669,8 @@ def tsv(self, 1 for monitoring progress and a string describing what is being done. """ - if meta_data is None: - meta_data = {} + # copy to not modify the dictionary passed by the caller + meta_data = dict(meta_data) if meta_data is not None else {} features = [c.lower() for c in features] features = sorted(set(features)) path = pathlib.Path(path) diff --git a/tests/test_rtdc_export_fcs.py b/tests/test_rtdc_export_fcs.py index 01b36ec0..7ba50b41 100644 --- a/tests/test_rtdc_export_fcs.py +++ b/tests/test_rtdc_export_fcs.py @@ -75,3 +75,14 @@ def test_fcs_not_filtered(): edest = tempfile.mkdtemp() f1 = join(edest, "test.tsv") ds.export.fcs(f1, keys, filtered=False) + + +def test_fcs_export_meta_data_not_modified(tmp_path): + """The `meta_data` dictionary of the caller must not be modified""" + pytest.importorskip("fcswrite") + keys = ["area_um", "deform"] + ddict = example_data_dict(size=222, keys=keys) + ds = dclab.new_dataset(ddict) + meta_data = {"sample": "test"} + ds.export.fcs(tmp_path / "test.fcs", keys, meta_data=meta_data) + assert meta_data == {"sample": "test"} diff --git a/tests/test_rtdc_export_tsv.py b/tests/test_rtdc_export_tsv.py index 494cdb19..8f27505e 100644 --- a/tests/test_rtdc_export_tsv.py +++ b/tests/test_rtdc_export_tsv.py @@ -114,3 +114,13 @@ def test_tsv_not_filtered(tmp_path): f1 = tmp_path / "test.tsv" ds.export.tsv(f1, keys, filtered=False) + + +def test_tsv_export_meta_data_not_modified(tmp_path): + """The `meta_data` dictionary of the caller must not be modified""" + keys = ["area_um", "deform"] + ddict = example_data_dict(size=222, keys=keys) + ds = dclab.new_dataset(ddict) + meta_data = {"sample": "test"} + ds.export.tsv(tmp_path / "test.tsv", keys, meta_data=meta_data) + assert meta_data == {"sample": "test"}