diff --git a/autofit/mapper/prior/arithmetic/assertion.py b/autofit/mapper/prior/arithmetic/assertion.py index 20b1a20e5..53acc5019 100644 --- a/autofit/mapper/prior/arithmetic/assertion.py +++ b/autofit/mapper/prior/arithmetic/assertion.py @@ -86,6 +86,12 @@ def _instance_for_arguments( class CompoundAssertion(AbstractPriorModel, Compound): + 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 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)