From a02c1521759c7f829908a4b80616beb3cc60145e Mon Sep 17 00:00:00 2001 From: nanglo123 Date: Wed, 2 Oct 2024 15:45:44 -0400 Subject: [PATCH] Create a copy of the unit dictionary --- mira/metamodel/units.py | 11 ++++++----- tests/test_modeling/test_amr_ops.py | 7 +++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/mira/metamodel/units.py b/mira/metamodel/units.py index 31c2af9f..1b28ec2f 100644 --- a/mira/metamodel/units.py +++ b/mira/metamodel/units.py @@ -44,11 +44,12 @@ class Unit(BaseModel): def from_json(cls, data: Dict[str, Any]) -> "Unit": # Use get_sympy from amr.petrinet, but avoid circular import from mira.sources.amr.petrinet import get_sympy - data["expression"] = get_sympy(data, local_dict=UNIT_SYMBOLS) - assert data.get('expression') is None or not isinstance( - data['expression'], str - ) - return cls(**data) + new_data = data.copy() + new_data["expression"] = get_sympy(data, local_dict=UNIT_SYMBOLS) + assert (new_data.get('expression') is None or + not isinstance(new_data.get('expression'), str)) + + return cls(**new_data) @classmethod def model_validate(cls, obj): diff --git a/tests/test_modeling/test_amr_ops.py b/tests/test_modeling/test_amr_ops.py index e194ed27..666534af 100644 --- a/tests/test_modeling/test_amr_ops.py +++ b/tests/test_modeling/test_amr_ops.py @@ -1,8 +1,10 @@ import unittest import requests from copy import deepcopy as _d + from mira.modeling.amr.ops import * from mira.metamodel.io import mathml_to_expression +from mira.metamodel import safe_parse_expr try: import sbmlmath @@ -270,8 +272,9 @@ def test_replace_parameter_id(self): self.assertEqual(old_param_dict[old_id]['value'], new_param_dict[new_id]['value']) self.assertEqual(old_param_dict[old_id]['distribution'], new_param_dict[new_id]['distribution']) - self.assertEqual(str(old_param_dict[old_id]['units']['expression']), - new_param_dict[new_id]['units']['expression']) + self.assertEqual(safe_parse_expr(old_param_dict[old_id]['units']['expression']), + safe_parse_expr(new_param_dict[new_id]['units'][ + 'expression'])) self.assertEqual(mathml_to_expression(old_param_dict[old_id]['units']['expression_mathml']), mathml_to_expression(new_param_dict[new_id]['units']['expression_mathml']))