From d7abda65db237b03ba4b02e08c8f6a27e202375f Mon Sep 17 00:00:00 2001
From: FZill <florian.zill@ufz.de>
Date: Thu, 21 Mar 2024 12:45:26 +0100
Subject: [PATCH] [meshlib] introducing typechecked

---
 ogstools/meshlib/boundary_subset.py | 13 +++----------
 ogstools/meshlib/data_processing.py |  9 ++-------
 pyproject.toml                      |  1 +
 3 files changed, 6 insertions(+), 17 deletions(-)

diff --git a/ogstools/meshlib/boundary_subset.py b/ogstools/meshlib/boundary_subset.py
index 6a7f7b6a2..ff8bb6d7c 100644
--- a/ogstools/meshlib/boundary_subset.py
+++ b/ogstools/meshlib/boundary_subset.py
@@ -5,6 +5,7 @@ from typing import Union
 import numpy as np
 import pyvista as pv
 from ogs import cli
+from typeguard import typechecked
 
 
 class Surface:
@@ -19,11 +20,8 @@ class Surface:
     def material_id(self) -> int:
         return self._material_id
 
-    def __init__(
-        self,
-        input: Union[Path, pv.DataSet],
-        material_id: int,
-    ):
+    @typechecked
+    def __init__(self, input: Union[Path, pv.DataSet], material_id: int):
         """Initialize a surface mesh. Either from pyvista or from a file."""
         self._material_id = material_id
 
@@ -37,11 +35,6 @@ class Surface:
             self.mesh = input
             self.filename = Path(tempfile.mkstemp(".vtu", "surface")[1])
             pv.save_meshio(self.filename, self.mesh, file_format="vtu")
-        else:
-            msg = "{} given, must be either Path or pyvista Dataset.".format(
-                self.filename
-            )
-            raise ValueError(msg)
 
         self.mesh.cell_data["MaterialIDs"] = (
             np.ones(self.mesh.n_cells) * self.material_id
diff --git a/ogstools/meshlib/data_processing.py b/ogstools/meshlib/data_processing.py
index eb6fa3614..9b65e77d4 100644
--- a/ogstools/meshlib/data_processing.py
+++ b/ogstools/meshlib/data_processing.py
@@ -3,6 +3,7 @@ from typing import Optional, Union
 
 import numpy as np
 import pyvista as pv
+from typeguard import typechecked
 
 from ogstools.propertylib import Property
 
@@ -85,7 +86,7 @@ def difference_pairwise(
     )
 
 
-# TODO: let this also return meshes and a property
+@typechecked
 def difference_matrix(
     meshes_1: Union[list, np.ndarray],
     meshes_2: Optional[Union[list, np.ndarray]] = None,
@@ -104,13 +105,7 @@ def difference_matrix(
                 or all datasets between meshes_1 and meshes_2 for all possible
                 combinations.
     """
-    if not isinstance(meshes_1, (list, np.ndarray)):
-        msg = "mesh1 is neither of type list nor np.ndarray"  # type: ignore[unreachable]
-        raise TypeError(msg)
     meshes_1 = np.asarray(meshes_1).flatten()
-    if not isinstance(meshes_2, (list, np.ndarray)) and meshes_2 is not None:
-        msg = "mesh2 is neither of type list nor np.ndarray."  # type: ignore[unreachable]
-        raise TypeError(msg)
     if meshes_2 is None:
         meshes_2 = meshes_1.copy()
     meshes_2 = np.asarray(meshes_2).flatten()
diff --git a/pyproject.toml b/pyproject.toml
index 3ff6c2b35..f1d5d22d8 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -26,6 +26,7 @@ dependencies = [
   "ogs>=6.5.1.dev0",
   "ogs6py>=0.370",
   "tqdm>=4.60",
+  "typeguard>=4.0.0",
   "VTUinterface>=0.704",
 ]
 
-- 
GitLab