From 45474dcf7291213e0393d07610c3f53dcb880dcd Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Sun, 25 Oct 2026 07:24:39 -0700 Subject: [PATCH] fix: Preserve user-supplied assertion names --- autofit/mapper/prior/arithmetic/assertion.py | 10 ++++++ autofit/mapper/prior_model/abstract.py | 2 +- test_autofit/graph_spec/test_catalogue.py | 5 ++- .../mapper/prior/test_assertion_names.py | 35 +++++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 test_autofit/mapper/prior/test_assertion_names.py diff --git a/autofit/mapper/prior/arithmetic/assertion.py b/autofit/mapper/prior/arithmetic/assertion.py index 20b1a20e5..6d2d10450 100644 --- a/autofit/mapper/prior/arithmetic/assertion.py +++ b/autofit/mapper/prior/arithmetic/assertion.py @@ -11,6 +11,11 @@ def __init__(self, lower, greater, name=""): super().__init__(lower, greater) self._name = name + @property + def name(self): + """The optional label supplied when this assertion was added.""" + return self._name + def __gt__(self, other): return CompoundAssertion(self, self._left > other) @@ -92,6 +97,11 @@ def __init__(self, assertion_1, assertion_2, name=""): self.assertion_2 = assertion_2 self._name = name + @property + def name(self): + """The optional label supplied when this assertion was added.""" + return self._name + def _instance_for_arguments( self, arguments, diff --git a/autofit/mapper/prior_model/abstract.py b/autofit/mapper/prior_model/abstract.py index bbea7b6d0..47e29a5db 100644 --- a/autofit/mapper/prior_model/abstract.py +++ b/autofit/mapper/prior_model/abstract.py @@ -744,7 +744,7 @@ def add_assertion(self, assertion, name=""): if assertion is True: return try: - assertion.name = name + assertion._name = name except AttributeError: pass self._assertions.append(assertion) diff --git a/test_autofit/graph_spec/test_catalogue.py b/test_autofit/graph_spec/test_catalogue.py index a20686501..7d028dfc9 100644 --- a/test_autofit/graph_spec/test_catalogue.py +++ b/test_autofit/graph_spec/test_catalogue.py @@ -211,9 +211,8 @@ def test_06_assertion(): assert edge.op == "<" assert edge.left == "0.5" assert edge.right == "sigma" - # `add_assertion(name=)` is silently dropped by `AbstractPriorModel.name` - # having no setter (a separately filed bug), so the name is absent. - assert edge.name is None + # The graph carries the user-supplied assertion label. + assert edge.name == "sigma is large" # Assertions are edges: not in the tree, and not in `model.info`. assert [row.name for row in spec.root.rows] == ["centre", "normalization", "sigma"] diff --git a/test_autofit/mapper/prior/test_assertion_names.py b/test_autofit/mapper/prior/test_assertion_names.py new file mode 100644 index 000000000..5b7a050e3 --- /dev/null +++ b/test_autofit/mapper/prior/test_assertion_names.py @@ -0,0 +1,35 @@ +import autofit as af +import pytest + + +@pytest.mark.parametrize("chained", [False, True]) +@pytest.mark.parametrize("name", ["", "sigma_floor"]) +def test_added_assertion_name_and_violation(chained, name): + model = af.Model(af.ex.Gaussian) + assertion = (5.0 < model.sigma) < 10.0 if chained else model.sigma > 5.0 + model.add_assertion(assertion, name=name) + assert model.gathered_assertions()[0].name == name + with pytest.raises(af.exc.FitException) as error: + model.check_assertions({model.sigma: 1.0}) + assert name in str(error.value) + + +def test_name_is_presentation_only(): + model = af.Model(af.ex.Gaussian) + assertion = model.sigma > 5.0 + model.add_assertion(assertion) + identifier = model.identifier + assertion._name = "sigma_floor" + assert assertion.name == "sigma_floor" + assert model.identifier == identifier + assert model.name == "Gaussian" + assert af.Collection().name == "Collection" + + +def test_boolean_assertions_keep_their_behaviour(): + model = af.Model(af.ex.Gaussian) + model.add_assertion(True, name="true") + assert model.gathered_assertions() == [] + model.add_assertion(False, name="false") + with pytest.raises(af.exc.FitException): + model.check_assertions({})