diff --git a/CHANGELOG.md b/CHANGELOG.md index 762a23d019..12a7d921d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ ## Bug Fixes +- Added `_from_json()` functionality to `Sign` which was erroneously omitted previously. ([#4517](https://github.com/pybamm-team/PyBaMM/pull/4517)) - Fixed bug where IDAKLU solver failed when `output variables` were specified and an extrapolation event is present. ([#4440](https://github.com/pybamm-team/PyBaMM/pull/4440)) ## Breaking changes diff --git a/src/pybamm/expression_tree/unary_operators.py b/src/pybamm/expression_tree/unary_operators.py index ace1cd9942..aa90fd6f4c 100644 --- a/src/pybamm/expression_tree/unary_operators.py +++ b/src/pybamm/expression_tree/unary_operators.py @@ -212,7 +212,8 @@ def __init__(self, child): @classmethod def _from_json(cls, snippet: dict): - raise NotImplementedError() + """See :meth:`pybamm.UnaryOperator._from_json()`.""" + return cls(snippet["children"][0]) def diff(self, variable): """See :meth:`pybamm.Symbol.diff()`.""" diff --git a/tests/unit/test_expression_tree/test_unary_operators.py b/tests/unit/test_expression_tree/test_unary_operators.py index 0dbafa38c5..d7544763fe 100644 --- a/tests/unit/test_expression_tree/test_unary_operators.py +++ b/tests/unit/test_expression_tree/test_unary_operators.py @@ -138,9 +138,20 @@ def test_sign(self): ) # Test from_json - with pytest.raises(NotImplementedError): - # signs are always scalar/array types in a discretised model - pybamm.Sign._from_json({}) + c = pybamm.Multiplication(pybamm.Variable("a"), pybamm.Scalar(3)) + sign_json = { + "name": "sign", + "id": 5341515228900508018, + "domains": { + "primary": [], + "secondary": [], + "tertiary": [], + "quaternary": [], + }, + "children": [c], + } + + assert pybamm.sign(c) == pybamm.Sign._from_json(sign_json) def test_floor(self, mocker): a = pybamm.Symbol("a")