Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
entry: bash -c "poetry run mypy ."
language: system
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.0
rev: v0.15.20
hooks:
- id: ruff
- id: ruff-format
Expand Down
3 changes: 3 additions & 0 deletions ixmp4/core/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from ixmp4.data.meta.service import RunMetaEntryService

from ..data.meta.type import check_meta_type
from .base import BaseServiceFacade

if TYPE_CHECKING:
Expand Down Expand Up @@ -114,6 +115,7 @@ def __setitem__(self, key: str, value: MetaValueType | np.generic | None) -> Non
pass

py_value = numpy_to_pytype(value)
check_meta_type(py_value)
if py_value is not None:
self._service.create(self.run.id, key, py_value)
self.df, self.data = self._get()
Expand Down Expand Up @@ -197,6 +199,7 @@ def __set__(
df = pd.DataFrame(
{"key": value.keys(), "value": [numpy_to_pytype(v) for v in value.values()]}
)
check_meta_type(df.value)
df.dropna(axis=0, inplace=True)
df["run__id"] = obj._dto.id
obj._backend.meta.bulk_upsert(df)
Expand Down
18 changes: 17 additions & 1 deletion ixmp4/data/meta/type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from enum import Enum
from typing import Literal
from typing import Any, Literal

from pandas.core.dtypes.inference import is_list_like

from ixmp4.data.meta.exceptions import InvalidRunMeta

PdDtype = Literal["Int64", "str", "float64", "boolean"]

Expand Down Expand Up @@ -30,6 +34,18 @@ def __str__(self) -> str:
return self.value


def check_meta_type(value: Any) -> None:
if is_list_like(value):
for v in value:
check_meta_type(v)
return

if type(value) not in _type_map and value is not None:
raise InvalidRunMeta(
f"Invalid meta indicator '{value}', allowed types: {list(_type_map.keys())}"
)


_type_map: dict[type, Type] = {
int: Type.INT,
str: Type.STR,
Expand Down
21 changes: 21 additions & 0 deletions tests/core/test_meta.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import re

import numpy as np
import pandas as pd
import pandas.testing as pdt
import pytest

import ixmp4
from ixmp4.data.meta.exceptions import InvalidRunMeta
from tests import backends
from tests.custom_exception import CustomException

Expand Down Expand Up @@ -37,6 +40,24 @@ def test_add_meta(self, platform: ixmp4.Platform, run: ixmp4.Run) -> None:
run2 = platform.runs.get("Model", "Scenario")
assert dict(run2.meta) == {"mint": 13, "mfloat": -1.9, "mstr": "foo"}

def test_add_meta_invalid_type(self, platform: ixmp4.Platform) -> None:

run = platform.runs.create("Model", "Scenario")

match = re.escape(
"Invalid meta indicator '2026-06-25 01:00:00', allowed types: "
"[<class 'int'>, <class 'str'>, <class 'float'>, <class 'bool'>]"
)
datetime_value = pd.to_datetime("2026-6-25 01:00")

with pytest.raises(InvalidRunMeta, match=match):
with run.transact("Add meta indicator"):
run.meta = {"mstr": "foo", "mdatetime": datetime_value} # type: ignore

with pytest.raises(InvalidRunMeta, match=match):
with run.transact("Add meta indicator"):
run.meta["mdatetime"] = datetime_value # type: ignore

def test_tabulate_platform_meta_after_add(self, platform: ixmp4.Platform) -> None:
exp = pd.DataFrame(
[
Expand Down
Loading