Skip to content

Commit

Permalink
Address more issues with from_json
Browse files Browse the repository at this point in the history
  • Loading branch information
bgyori committed Oct 3, 2024
1 parent 06a9e87 commit d417351
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions mira/metamodel/template_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import copy

from pydantic import ConfigDict

__all__ = [
Expand Down Expand Up @@ -473,6 +475,8 @@ def from_json(cls, data) -> "TemplateModel":
:
Returns the newly created template model.
"""
# Do a copy just to make sure we don't modify the original data
data = copy.deepcopy(data)
local_symbols = {p: sympy.Symbol(p) for p in data.get("parameters", [])}
for template_dict in data.get("templates", []):
# We need to figure out the template class based on the type
Expand Down
6 changes: 6 additions & 0 deletions mira/metamodel/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Regenerate the JSON schema by running ``python -m mira.metamodel.schema``.
"""
import copy

from pydantic import ConfigDict
from typing import Literal

Expand Down Expand Up @@ -376,6 +378,8 @@ def from_json(cls, data) -> "Concept":
if isinstance(data, Concept):
return data
elif data.get('units'):
# Copy so we don't update the input
data = copy.deepcopy(data)
data['units'] = Unit.from_json(data['units'])

return cls(**data)
Expand Down Expand Up @@ -413,6 +417,8 @@ def from_json(cls, data, rate_symbols=None) -> "Template":
:
A Template object
"""
# Make a copy to make sure we don't update the input
data = copy.deepcopy(data)
# We make sure to use data such that it's not modified in place,
# e.g., we don't use pop or overwrite items, otherwise this function
# would have unintended side effects.
Expand Down
4 changes: 2 additions & 2 deletions mira/metamodel/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class Unit(BaseModel):

@classmethod
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
# Use get_sympy from sources, but avoid circular import
from mira.sources.util import get_sympy
new_data = data.copy()
new_data["expression"] = get_sympy(data, local_dict=UNIT_SYMBOLS)
assert (new_data.get('expression') is None or
Expand Down

0 comments on commit d417351

Please sign in to comment.