From bdd638e82bc74c1b8ac8e6f24df0efe2ac5de171 Mon Sep 17 00:00:00 2001 From: Maarten Pronk Date: Fri, 19 Jan 2024 12:20:01 +0100 Subject: [PATCH] Move to PrivateAttr. --- python/ribasim/ribasim/input_base.py | 7 ++++--- python/ribasim/tests/test_io.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/python/ribasim/ribasim/input_base.py b/python/ribasim/ribasim/input_base.py index ef3389b87..97d020f1e 100644 --- a/python/ribasim/ribasim/input_base.py +++ b/python/ribasim/ribasim/input_base.py @@ -20,6 +20,7 @@ ConfigDict, DirectoryPath, Field, + PrivateAttr, ValidationInfo, field_validator, model_serializer, @@ -159,7 +160,7 @@ def _load(cls, filepath: Path | None) -> dict[str, Any]: class TableModel(FileModel, Generic[TableT]): df: DataFrame[TableT] | None = Field(default=None, exclude=True, repr=False) - sort_keys: list[str] = Field(default=[], exclude=True, repr=False) + _sort_keys: list[str] = PrivateAttr(default=[]) @field_validator("df") @classmethod @@ -296,7 +297,7 @@ def sort(self): Sorting is done automatically before writing the table. """ if self.df is not None: - self.df.sort_values(self.sort_keys, ignore_index=True, inplace=True) + self.df.sort_values(self._sort_keys, ignore_index=True, inplace=True) @classmethod def tableschema(cls) -> TableT: @@ -408,7 +409,7 @@ def set_sort_keys(cls, v: Any, info: ValidationInfo) -> Any: field = cls.model_fields[getattr(info, "field_name")] extra = field.json_schema_extra if extra is not None and isinstance(extra, dict): - v.sort_keys = extra.get("sort_keys", []) # type: ignore + v._sort_keys = extra.get("sort_keys", []) # type: ignore return v @classmethod diff --git a/python/ribasim/tests/test_io.py b/python/ribasim/tests/test_io.py index f6cf6c5da..e86b9456b 100644 --- a/python/ribasim/tests/test_io.py +++ b/python/ribasim/tests/test_io.py @@ -111,7 +111,7 @@ def test_sort(level_setpoint_with_minmax, tmp_path): # apply a wrong sort, then call the sort method to restore order table.df.sort_values("greater_than", ascending=False, inplace=True) assert table.df.iloc[0]["greater_than"] == 15.0 - assert table.sort_keys == [ + assert table._sort_keys == [ "node_id", "listen_feature_id", "variable",