Skip to content

Commit

Permalink
Move to PrivateAttr.
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion committed Jan 19, 2024
1 parent c24238c commit bdd638e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions python/ribasim/ribasim/input_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ConfigDict,
DirectoryPath,
Field,
PrivateAttr,
ValidationInfo,
field_validator,
model_serializer,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python/ribasim/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit bdd638e

Please sign in to comment.