From adacfb113e357942c1f2db66e6067242efaede6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Thu, 19 Nov 2026 10:06:19 +0100 Subject: [PATCH] fix: `hdf5_autocomplete_config` raised AttributeError for path input The function called `.close()` on the str/pathlib.Path object instead of closing the RTDCWriter (and thus the file it opened). Co-Authored-By: Claude Opus 5.5 --- CHANGELOG | 3 +++ dclab/rtdc_dataset/export.py | 12 +++--------- tests/test_rtdc_export_hdf5.py | 27 ++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 50e63cbf..44c1d7d0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -266,6 +266,9 @@ 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) + - fix: deprecated `export.hdf5_autocomplete_config` raised an + AttributeError when given a path (`.close()` was called on the path + instead of on the file opened by `RTDCWriter`) (#423) 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 f1c7d6ba..d160381f 100644 --- a/dclab/rtdc_dataset/export.py +++ b/dclab/rtdc_dataset/export.py @@ -9,7 +9,6 @@ import uuid import warnings -import h5py import hdf5plugin try: @@ -932,13 +931,8 @@ def hdf5_autocomplete_config(path_or_h5obj): "the dclab.RTDCWriter context manager or the " "dclab.RTDCWriter.rectify_metadata function.", DeprecationWarning) - if not isinstance(path_or_h5obj, h5py.File): - close = True - else: - close = False - hw = RTDCWriter(path_or_h5obj, mode="append") hw.rectify_metadata() - - if close: - path_or_h5obj.close() + # Only closes the file if `path_or_h5obj` is a path (an h5py.File + # passed by the caller is left open). + hw.close() diff --git a/tests/test_rtdc_export_hdf5.py b/tests/test_rtdc_export_hdf5.py index 99f3961c..bff744b9 100644 --- a/tests/test_rtdc_export_hdf5.py +++ b/tests/test_rtdc_export_hdf5.py @@ -12,7 +12,7 @@ import dclab from dclab import dfn, new_dataset, RTDCWriter -from dclab.rtdc_dataset import rtdc_copy, writer +from dclab.rtdc_dataset import export, rtdc_copy, writer from dclab.rtdc_dataset.export import ( ContourNotExportedWarning, store_filtered_feature, @@ -1418,3 +1418,28 @@ def test_hdf5_traces_filter2(): assert np.all(ds["trace"]["fl1_raw"][3] == ds2["trace"]["fl1_raw"][0]) assert np.all(ds["trace"]["fl1_raw"][5] == ds2["trace"]["fl1_raw"][1]) assert np.all(ds["trace"]["fl1_raw"][6] == ds2["trace"]["fl1_raw"][2]) + + +@pytest.mark.parametrize("path_type", [str, pathlib.Path, "h5py"]) +def test_hdf5_autocomplete_config_deprecated(tmp_path, path_type): + """The deprecated `hdf5_autocomplete_config` works with paths""" + path = tmp_path / "test.rtdc" + with dclab.RTDCWriter(path) as hw: + hw.store_metadata({"experiment": {"sample": "test", + "run index": 1}}) + hw.store_feature("deform", np.linspace(0.01, 0.1, 10)) + with h5py.File(path, "a") as h5: + del h5.attrs["experiment:event count"] + + if path_type == "h5py": + with h5py.File(path, "a") as h5: + with pytest.warns(DeprecationWarning, match="deprecated"): + export.hdf5_autocomplete_config(h5) + # the file object passed by the user is not closed + assert h5.id.valid + else: + with pytest.warns(DeprecationWarning, match="deprecated"): + export.hdf5_autocomplete_config(path_type(path)) + + with h5py.File(path, "r") as h5: + assert h5.attrs["experiment:event count"] == 10