From 9459ac2d121c159b85792a7b022639d4d05f229d Mon Sep 17 00:00:00 2001 From: Matthew Wardrop Date: Fri, 9 Sep 2022 19:58:47 -0700 Subject: [PATCH] Fix casting `Formula` objects to strings. --- formulaic/formula.py | 2 +- tests/test_formula.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/formulaic/formula.py b/formulaic/formula.py index 466f047c..3a9044b4 100644 --- a/formulaic/formula.py +++ b/formulaic/formula.py @@ -251,7 +251,7 @@ def __getitem__(self, key): return Formula.from_spec(subformula) return subformula - def __repr__(self): # pylint: disable=signature-differs + def __repr__(self, to_str: bool = False): if not self._has_structure and self._has_root: return " + ".join([str(t) for t in self]) return str(self._map(lambda terms: " + ".join([str(t) for t in terms]))) diff --git a/tests/test_formula.py b/tests/test_formula.py index 11992cba..d024a3fd 100644 --- a/tests/test_formula.py +++ b/tests/test_formula.py @@ -121,6 +121,8 @@ def test_repr(self, formula_expr, formula_exprs): assert repr(Formula("a | b")) == ( "root:\n [0]:\n 1 + a\n [1]:\n 1 + b" ) + assert str(formula_expr) == "1 + a + b + c + a:b + a:c + b:c + a:b:c" + assert str(formula_exprs) == ".lhs:\n a\n.rhs:\n 1 + b" def test_equality(self): assert Formula("a + b") == Formula("a+b")