Skip to content

Commit

Permalink
Enforce data type hints (#105 | GRIDEDIT-760)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-el-sayed authored Oct 24, 2023
1 parent af16669 commit 9387327
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
python -m pip install numpy
python -m pip install matplotlib
python -m pip install pytest
python -m pip install type_enforced
# Step: Install system-provided dependencies
# macOS
Expand Down
32 changes: 17 additions & 15 deletions meshkernel/meshkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from typing import Callable, Tuple

import numpy as np
import type_enforced
from numpy import ndarray
from numpy.ctypeslib import as_ctypes

Expand Down Expand Up @@ -56,6 +57,7 @@
logger = logging.getLogger(__name__)


@type_enforced.Enforcer
class MeshKernel:
"""This class is the entry point for interacting with the MeshKernel library"""

Expand Down Expand Up @@ -86,14 +88,14 @@ def __init__(self, projection: ProjectionType = ProjectionType.CARTESIAN):

self.lib = CDLL(str(lib_path))

self.exit_code = self.__get_exit_codes()
self._exit_code = self.__get_exit_codes()

self._allocate_state(projection)

def __del__(self):
self._deallocate_state()

def __get_exit_codes(self) -> IntEnum:
def __get_exit_codes(self):
"""Stores the backend exit codes
Returns:
An integer enumeration called exit_code holding the exit codes of the backend
Expand Down Expand Up @@ -129,7 +131,7 @@ def __get_exit_codes(self) -> IntEnum:
self.lib.mkernel_get_exit_code_unknown_exception(byref(unknown_exception))

return IntEnum(
"exit_code",
"_exit_code",
{
"SUCCESS": success.value,
"MESHKERNEL_ERROR": meshkernel_error.value,
Expand Down Expand Up @@ -1066,7 +1068,7 @@ def mesh2d_get_nodes_in_polygons(
return selected_nodes

def _mesh2d_count_nodes_in_polygons(
self, geometry_list: GeometryList, inside: int
self, geometry_list: GeometryList, inside: bool
) -> int:
"""For internal use only.
Expand Down Expand Up @@ -1546,7 +1548,7 @@ def mkernel_get_inner_outer_separator(self) -> float:

return self.lib.mkernel_get_inner_outer_separator()

def _execute_function(self, function: Callable, *args):
def _execute_function(self, function, *args):
"""Utility function to execute a C function of MeshKernel and checks its status.
Args:
Expand All @@ -1558,25 +1560,25 @@ def _execute_function(self, function: Callable, *args):
if the MeshKernel library reports an error.
"""
exit_code = function(*args)
if exit_code != self.exit_code.SUCCESS:
if exit_code != self._exit_code.SUCCESS:
error_message = self._get_error()
if exit_code == self.exit_code.MESHKERNEL_ERROR:
if exit_code == self._exit_code.MESHKERNEL_ERROR:
raise MeshKernelError("MeshKernelError", error_message)
elif exit_code == self.exit_code.NOT_IMPLEMENTED_ERROR:
elif exit_code == self._exit_code.NOT_IMPLEMENTED_ERROR:
raise MeshKernelError("NotImplementedError", error_message)
elif exit_code == self.exit_code.ALGORITHM_ERROR:
elif exit_code == self._exit_code.ALGORITHM_ERROR:
raise MeshKernelError("AlgorithmError", error_message)
elif exit_code == self.exit_code.CONSTRAINT_ERROR:
elif exit_code == self._exit_code.CONSTRAINT_ERROR:
raise MeshKernelError("ConstraintError", error_message)
elif exit_code == self.exit_code.MESH_GEOMETRY_ERROR:
elif exit_code == self._exit_code.MESH_GEOMETRY_ERROR:
raise MeshGeometryError(error_message, self._get_geometry_error())
elif exit_code == self.exit_code.LINEAR_ALGEBRA_ERROR:
elif exit_code == self._exit_code.LINEAR_ALGEBRA_ERROR:
raise MeshKernelError("LinearAlgebraError", error_message)
elif exit_code == self.exit_code.RANGE_ERROR:
elif exit_code == self._exit_code.RANGE_ERROR:
raise MeshKernelError("RangeError", error_message)
elif exit_code == self.exit_code.STDLIB_EXCEPTION:
elif exit_code == self._exit_code.STDLIB_EXCEPTION:
raise MeshKernelError("STDLibException", error_message)
elif exit_code == self.exit_code.UNKNOWN_EXCEPTION:
elif exit_code == self._exit_code.UNKNOWN_EXCEPTION:
raise MeshKernelError("UnknownException", error_message)

def _curvilineargrid_get_dimensions(self) -> CCurvilinearGrid:
Expand Down
3 changes: 1 addition & 2 deletions scripts/build_deps.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#build wheels
PYBIN=/opt/python/cp38-cp38/bin/
rm -rf build *.egg-info
${PYBIN}/pip install numpy
${PYBIN}/pip install matplotlib
${PYBIN}/python3 -m pip install numpy matplotlib type_enforced
${PYBIN}/python3 setup.py build_ext || exit 1
${PYBIN}/python3 setup.py sdist bdist_wheel || exit 1
cd dist/
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def build_cmake(self, ext):
url="https://github.com/Deltares/MeshKernelPy",
license="MIT",
platforms="Windows, Linux, macOS",
install_requires=["numpy"],
install_requires=["numpy", "type_enforced"],
extras_require={
"tests": ["pytest", "pytest-cov", "nbval", "matplotlib"],
"lint": [
Expand Down
8 changes: 7 additions & 1 deletion tests/test_mesh2d_basics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from ctypes import c_int

import numpy as np
import pytest
Expand Down Expand Up @@ -1555,7 +1556,12 @@ def test_mesh2d_delete_small_flow_edges_and_small_triangles_delete_small_triangl


cases_nodes_in_polygons_mesh2d = [
(np.array([1.5, 2.5, 2.5, 1.5, 1.5]), np.array([1.5, 1.5, 2.5, 2.5, 1.5]), True, 1),
(
np.array([1.5, 2.5, 2.5, 1.5, 1.5]),
np.array([1.5, 1.5, 2.5, 2.5, 1.5]),
True,
1,
),
(
np.array([1.5, 2.5, 2.5, 1.5, 1.5]),
np.array([1.5, 1.5, 2.5, 2.5, 1.5]),
Expand Down
10 changes: 8 additions & 2 deletions tests/test_orthogonalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
from pytest import approx
from test_utils import Mesh2dFactory

from meshkernel import GeometryList, Mesh2d, MeshKernel, OrthogonalizationParameters
from meshkernel import (
GeometryList,
Mesh2d,
MeshKernel,
OrthogonalizationParameters,
ProjectToLandBoundaryOption,
)


def test_mesh2d_compute_orthogonalization():
Expand Down Expand Up @@ -41,7 +47,7 @@ def test_mesh2d_compute_orthogonalization():
land_boundary = GeometryList(land_boundary_x, land_boundary_y)

mk.mesh2d_compute_orthogonalization(
project_to_land_boundary_option=0,
project_to_land_boundary_option=ProjectToLandBoundaryOption.DO_NOT_PROJECT_TO_LANDBOUNDARY,
orthogonalization_parameters=OrthogonalizationParameters(outer_iterations=10),
land_boundaries=land_boundary,
selecting_polygon=polygon,
Expand Down

0 comments on commit 9387327

Please sign in to comment.