From 116069f692aa0b5a0f81426c7acc0d01a5dfa008 Mon Sep 17 00:00:00 2001 From: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> Date: Thu, 28 Mar 2024 17:34:15 +0100 Subject: [PATCH] Stop specifying own `FilePath` type (#1335) --- python/ribasim/ribasim/input_base.py | 11 +++++------ python/ribasim/ribasim/model.py | 12 +++++------- python/ribasim/ribasim/types.py | 5 ----- 3 files changed, 10 insertions(+), 18 deletions(-) delete mode 100644 python/ribasim/ribasim/types.py 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 f6d208890..ce19d7b24 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, ) @@ -125,9 +125,7 @@ def __repr__(self) -> str: content.append(")") return "\n".join(content) - def _write_toml(self, fn: FilePath): - fn = Path(fn) - + def _write_toml(self, fn: Path): 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())) @@ -201,11 +199,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. @@ -213,7 +211,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__ = ()