Skip to content

Commit 3f63510

Browse files
authored
[Python] Make sure filepaths are kept for external files. (#2943)
Fixes #2934
1 parent 56070cc commit 3f63510

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

python/ribasim/ribasim/input_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def _check_filepath(cls, value: Any, info: ValidationInfo) -> dict[str, Any]:
270270
# Skip loading when lazy (internal/external is False)
271271
# Otherwise load data and update our values
272272
# If no filepath is given, assume it is expected to be loaded from the database
273-
filepath = value.pop("filepath", None)
273+
filepath = value.get("filepath")
274274
if filepath is None and internal:
275275
filepath = cls.default_filepath()
276276
elif filepath is not None and external:
@@ -280,8 +280,8 @@ def _check_filepath(cls, value: Any, info: ValidationInfo) -> dict[str, Any]:
280280
return value
281281

282282
data = cls._load(dir / filepath)
283-
data.update(value)
284-
return data
283+
value.update(data)
284+
return value
285285
else:
286286
raise ValueError(f"Invalid type of value for FileModel: {type(value)}")
287287

python/ribasim/ribasim/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def _load(cls, filepath: Path | None) -> dict[str, object]:
539539
with filepath.open("rb") as f:
540540
config = tomli.load(f)
541541

542-
config["filepath"] = filepath
542+
config["filepath"] = filepath # make sure we store the whole filepath
543543
directory = filepath.parent / config["input_dir"]
544544
context_file_loading.get()["directory"] = directory
545545
_init_context_var.get()["directory"] = directory # type: ignore

python/ribasim/tests/test_input_base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_tablename():
2828
assert cls.tablename() == "Link"
2929

3030

31-
def test_filepath_appears_in_toml(tmp_path):
31+
def test_filepath_appears_in_toml(tmp_path, basic_arrow):
3232
"""Integration test: verify filepath is written to TOML and data to external file."""
3333
import tomli
3434

@@ -81,3 +81,9 @@ def test_filepath_appears_in_toml(tmp_path):
8181
assert "level" in ds.variables, "level should be in the dataset"
8282
assert "area" in ds.variables, "area should be in the dataset"
8383
assert len(ds["level"]) == 3, "Should have 3 profile points"
84+
85+
# Make sure external files show up in the TOML after reading
86+
assert basic_arrow.basin.profile.filepath == Path("profile.arrow")
87+
basic_arrow.write(tmp_path / "test_model_arrow" / "ribasim.toml")
88+
arrow_model = Model.read(tmp_path / "test_model_arrow" / "ribasim.toml")
89+
assert arrow_model.basin.profile.filepath == Path("profile.arrow")

0 commit comments

Comments
 (0)