From c7050382612a3b3de6bde8a8c1f9584469e94003 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Fri, 17 Jul 2026 07:56:28 +0100 Subject: [PATCH] fix: from_dict dropped dict entries with falsy values (e.g. 0.0) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The type=="dict" branch filtered entries with `if value`, silently dropping any entry whose deserialized value is falsy — including parameters exactly 0.0. Exposed by PYAUTO_TEST_MODE_SAMPLES (#1381) bypass output: lens centres/ell_comps have zero prior medians, so bypass-written summaries loaded with 8 of 15 parameters missing and al.agg reconstruction crashed with KeyError. Only None is skipped now. Regression test: zero-valued Sample kwargs round-trip. Co-Authored-By: Claude Fable 5 --- autofit/mapper/model_object.py | 2 +- test_autofit/serialise/test_samples.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/autofit/mapper/model_object.py b/autofit/mapper/model_object.py index 3316c7324..9756e51a6 100644 --- a/autofit/mapper/model_object.py +++ b/autofit/mapper/model_object.py @@ -213,7 +213,7 @@ def get_class_path(): loaded_ids=loaded_ids, ) for key, value in d["arguments"].items() - if value + if value is not None } elif type_ == "instance": class_path = get_class_path() diff --git a/test_autofit/serialise/test_samples.py b/test_autofit/serialise/test_samples.py index b3c745247..59e263b8b 100644 --- a/test_autofit/serialise/test_samples.py +++ b/test_autofit/serialise/test_samples.py @@ -157,3 +157,19 @@ def test_generic_from_dict(summary_dict): summary = from_dict(summary_dict) assert isinstance(summary, SamplesSummary) assert isinstance(summary.max_log_likelihood_sample, af.Sample) + + +def test_sample_zero_valued_kwargs_round_trip(): + """ + Sample kwargs whose value is exactly 0.0 (e.g. a prior-median centre) must + survive to_dict -> from_dict — the dict branch previously filtered falsy + values, silently dropping them. + """ + sample = af.Sample( + log_likelihood=1.0, + log_prior=0.0, + weight=1.0, + kwargs={"centre.centre_0": 0.0, "sigma": 6.0}, + ) + loaded = from_dict(to_dict(sample)) + assert loaded.kwargs == {("centre", "centre_0"): 0.0, ("sigma",): 6.0}