diff --git a/pyproject.toml b/pyproject.toml index f66f510..81033ec 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "infrasys" -version = "0.2.3" +version = "0.2.4" description = '' readme = "README.md" requires-python = ">=3.10, <3.13" diff --git a/src/infrasys/base_quantity.py b/src/infrasys/base_quantity.py index 6908c1e..a52fbfb 100644 --- a/src/infrasys/base_quantity.py +++ b/src/infrasys/base_quantity.py @@ -79,6 +79,8 @@ def to_dict(self) -> dict[str, Any]: val = self.magnitude if isinstance(self.magnitude, np.ndarray): val = self.magnitude.tolist() + elif isinstance(self.magnitude, np.generic): + val = val.item() return {"value": val, "units": str(self.units)} @classmethod diff --git a/tests/test_base_quantity.py b/tests/test_base_quantity.py index 4190901..d174ee7 100644 --- a/tests/test_base_quantity.py +++ b/tests/test_base_quantity.py @@ -1,3 +1,5 @@ +import os +from infrasys.system import System from pydantic import ValidationError from infrasys.base_quantity import ureg, BaseQuantity from infrasys.component import Component @@ -102,3 +104,14 @@ def test_custom_serialization(): model_dump = component.model_dump(mode="json", context={"magnitude_only": True}) assert model_dump["voltage"] == 10.0 + + +def test_system_save_with_pint_quantity(tmp_path): + component = BaseQuantityComponent(name="test", voltage=Voltage(np.float32(10.0), units="volt")) + system = System() + system.add_component(component) + custom_folder = "my_system" + + fpath = tmp_path / custom_folder / "test_system.json" + system.to_json(fpath) + assert os.path.exists(fpath), f"Folder {fpath} was not created successfully"