diff --git a/python/ribasim/ribasim/input_base.py b/python/ribasim/ribasim/input_base.py index d728b7462..99a5ec1b3 100644 --- a/python/ribasim/ribasim/input_base.py +++ b/python/ribasim/ribasim/input_base.py @@ -33,7 +33,6 @@ ) import ribasim -from ribasim.types import FilePath __all__ = ("TableModel",) @@ -286,7 +285,7 @@ def _write_arrow(self, filepath: Path, directory: Path, input_dir: Path) -> None ) @classmethod - def _from_db(cls, path: FilePath, table: str) -> pd.DataFrame | None: + def _from_db(cls, path: Path, table: str) -> pd.DataFrame | None: with connect(path) as connection: if exists(connection, table): query = f"select * from {esc_id(table)}" @@ -299,7 +298,7 @@ def _from_db(cls, path: FilePath, table: str) -> pd.DataFrame | None: return df @classmethod - def _from_arrow(cls, path: FilePath) -> pd.DataFrame: + def _from_arrow(cls, path: Path) -> pd.DataFrame: directory = context_file_loading.get().get("directory", Path(".")) return pd.read_feather(directory / path) @@ -358,7 +357,7 @@ class SpatialTableModel(TableModel[TableT], Generic[TableT]): df: GeoDataFrame[TableT] | None = Field(default=None, exclude=True, repr=False) @classmethod - def _from_db(cls, path: FilePath, table: str): + def _from_db(cls, path: Path, table: str): with connect(path) as connection: if exists(connection, table): df = gpd.read_file(path, layer=table, fid_as_index=True) @@ -367,13 +366,13 @@ def _from_db(cls, path: FilePath, table: str): return df - def _write_table(self, path: FilePath) -> None: + def _write_table(self, path: Path) -> None: """ Write the contents of the input to a database. Parameters ---------- - path : FilePath + path : Path """ assert self.df is not None self.df.to_file(path, layer=self.tablename(), driver="GPKG", mode="a") diff --git a/python/ribasim/ribasim/model.py b/python/ribasim/ribasim/model.py index ffbcf7829..902807bcf 100644 --- a/python/ribasim/ribasim/model.py +++ b/python/ribasim/ribasim/model.py @@ -1,5 +1,6 @@ import datetime from collections.abc import Generator +from os import PathLike from pathlib import Path from typing import Any @@ -11,7 +12,6 @@ from pydantic import ( DirectoryPath, Field, - FilePath, field_serializer, model_validator, ) @@ -128,7 +128,7 @@ def __repr__(self) -> str: content.append(")") return "\n".join(content) - def _write_toml(self, fn: FilePath) -> FilePath: + def _write_toml(self, fn: Path) -> Path: """ Write the model data to a TOML file. @@ -139,11 +139,9 @@ def _write_toml(self, fn: FilePath) -> FilePath: Returns ------- - FilePath + Path The file path of the written TOML file. """ - fn = Path(fn) - content = self.model_dump(exclude_unset=True, exclude_none=True, by_alias=True) # Filter empty dicts (default Nodes) content = dict(filter(lambda x: x[1], content.items())) @@ -217,11 +215,11 @@ def validate_model(self): self.validate_model_node_ids() @classmethod - def read(cls, filepath: FilePath) -> "Model": + def read(cls, filepath: str | PathLike[str]) -> "Model": """Read model from TOML file.""" return cls(filepath=filepath) # type: ignore - def write(self, filepath: Path | str) -> Path: + def write(self, filepath: str | PathLike[str]) -> Path: """ Write the contents of the model to disk and save it as a TOML configuration file. @@ -229,7 +227,7 @@ def write(self, filepath: Path | str) -> Path: Parameters ---------- - filepath: FilePath ending in .toml + filepath: str | PathLike[str] A file path with .toml extension """ # TODO # self.validate_model() diff --git a/python/ribasim/ribasim/types.py b/python/ribasim/ribasim/types.py deleted file mode 100644 index 407e07f53..000000000 --- a/python/ribasim/ribasim/types.py +++ /dev/null @@ -1,5 +0,0 @@ -from os import PathLike - -FilePath = str | PathLike[str] - -__all__ = ()