diff --git a/bilby/core/prior/dict.py b/bilby/core/prior/dict.py index 65688a620..30d5ee3c0 100644 --- a/bilby/core/prior/dict.py +++ b/bilby/core/prior/dict.py @@ -60,6 +60,14 @@ def __init__(self, dictionary=None, filename=None, conversion_function=None): def __hash__(self): return hash(str(self)) + def __setitem__(self, key, value): + if not isinstance(value, (Prior, int, float, str, dict)): + raise TypeError( + "Unable to parse prior, bad entry: {} " + "= {} of type {}".format(key, value, type(value)) + ) + super().__setitem__(key, value) + @xp_wrap def evaluate_constraints(self, sample, *, xp=None): out_sample = self.conversion_function(sample) diff --git a/test/core/prior/dict_test.py b/test/core/prior/dict_test.py index cdd996f19..aed594cee 100644 --- a/test/core/prior/dict_test.py +++ b/test/core/prior/dict_test.py @@ -79,6 +79,11 @@ def test_prior_set_has_correct_length(self): def test_prior_set_has_expected_priors(self): self.assertDictEqual(self.priors, dict(self.prior_set_from_dict)) + def test_assignment_rejects_invalid_prior_value(self): + expected_error = "Unable to parse prior, bad entry" + with self.assertRaisesRegex(TypeError, expected_error): + self.prior_set_from_dict["Om0"] = np.array(0.30966) + def test_read_from_file(self): expected = dict( mass_1=bilby.core.prior.Constraint(