From 537d57ad01cc0a61e29e68b7c8586db770ee3658 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Sun, 25 Oct 2026 07:24:36 -0700 Subject: [PATCH 1/2] fix: Make compound assertion representations safe --- autofit/mapper/prior/arithmetic/assertion.py | 6 ++++ autofit/mapper/prior/arithmetic/compound.py | 3 ++ .../mapper/prior/test_assertion_repr.py | 28 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 test_autofit/mapper/prior/test_assertion_repr.py diff --git a/autofit/mapper/prior/arithmetic/assertion.py b/autofit/mapper/prior/arithmetic/assertion.py index 20b1a20e5..be28c43c6 100644 --- a/autofit/mapper/prior/arithmetic/assertion.py +++ b/autofit/mapper/prior/arithmetic/assertion.py @@ -92,6 +92,12 @@ def __init__(self, assertion_1, assertion_2, name=""): self.assertion_2 = assertion_2 self._name = name + def __repr__(self): + return ( + f"{type(self).__name__}(" + f"{self.assertion_1!r}, {self.assertion_2!r})" + ) + def _instance_for_arguments( self, arguments, diff --git a/autofit/mapper/prior/arithmetic/compound.py b/autofit/mapper/prior/arithmetic/compound.py index 764cff6cd..c5805cc2b 100644 --- a/autofit/mapper/prior/arithmetic/compound.py +++ b/autofit/mapper/prior/arithmetic/compound.py @@ -84,6 +84,9 @@ def __init__(self, left, right): self.left = left self.right = right + def __str__(self): + return f"{type(self).__name__}(left={self.left!r}, right={self.right!r})" + def __repr__(self): return str(self) diff --git a/test_autofit/mapper/prior/test_assertion_repr.py b/test_autofit/mapper/prior/test_assertion_repr.py new file mode 100644 index 000000000..18d3f8c2f --- /dev/null +++ b/test_autofit/mapper/prior/test_assertion_repr.py @@ -0,0 +1,28 @@ +import autofit as af +import pytest + + +@pytest.mark.parametrize("kind", ["simple", "chained", "arithmetic"]) +def test_print_gathered_assertions(kind, capsys): + model = af.Model(af.ex.Gaussian) + if kind == "simple": + assertion = model.sigma > 5.0 + elif kind == "chained": + assertion = (0.0 < model.sigma) < 5.0 + else: + assertion = (model.sigma * 2.0) > 5.0 + model.add_assertion(assertion) + identifier = model.identifier + print(model.gathered_assertions()) + rendered = capsys.readouterr().out + assert type(assertion).__name__ in rendered + assert "5.0" in rendered + assert rendered.count("\n") == 1 + assert model.identifier == identifier + + +def test_compound_prior_fallback_preserves_specialised_strings(): + prior = af.UniformPrior() + assert "PowerPrior" in repr(prior ** 2.0) + assert repr(prior + 2.0) == str(prior + 2.0) + assert repr(prior * 2.0) == str(prior * 2.0) From 04ae11791d905eda26992f89b1084ddbc1617f52 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Sun, 25 Oct 2026 07:34:32 -0700 Subject: [PATCH 2/2] Keep assertion representation separate from name property changes --- autofit/mapper/prior/arithmetic/assertion.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/autofit/mapper/prior/arithmetic/assertion.py b/autofit/mapper/prior/arithmetic/assertion.py index be28c43c6..53acc5019 100644 --- a/autofit/mapper/prior/arithmetic/assertion.py +++ b/autofit/mapper/prior/arithmetic/assertion.py @@ -86,18 +86,18 @@ def _instance_for_arguments( class CompoundAssertion(AbstractPriorModel, Compound): - def __init__(self, assertion_1, assertion_2, name=""): - super().__init__() - self.assertion_1 = assertion_1 - self.assertion_2 = assertion_2 - self._name = name - def __repr__(self): return ( f"{type(self).__name__}(" f"{self.assertion_1!r}, {self.assertion_2!r})" ) + def __init__(self, assertion_1, assertion_2, name=""): + super().__init__() + self.assertion_1 = assertion_1 + self.assertion_2 = assertion_2 + self._name = name + def _instance_for_arguments( self, arguments,