Skip to content

Commit

Permalink
Raise on non-int arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
PProfizi committed Jan 27, 2025
1 parent 52b9db2 commit d03ce5e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/ansys/dpf/core/scoping.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@
from ansys.dpf.core.server_types import AnyServerType
import ansys.grpc.dpf.scoping_pb2.Scoping as ScopingMessage
from ctypes import c_void_p as ScopingPointer
from numpy import typing as np_typing

IdVectorType = Union[list[int], range]
IdVectorType = Union[list[int], range, np_typing.NDArray[np.int32]]


class Scoping:
Expand Down Expand Up @@ -195,8 +196,13 @@ def _set_ids(self, ids: IdVectorType):
if isinstance(ids, range):
ids = list(ids)
if isinstance(ids, np.ndarray):
if ids.dtype != np.int32:
if ids.dtype == np.int64:
ids = ids.astype(np.int32)
if ids.dtype != np.int32:
raise ValueError(
f"Accepted dtypes for NumPy arrays when setting scoping IDs are "
f"'np.int32' and np.int64' (provided is '{ids.dtype}')."
)
if isinstance(self._server, server_types.InProcessServer):
self._api.scoping_resize(self, len(ids))
ids_ptr = self._api.scoping_get_ids(self, len(ids))
Expand Down
8 changes: 8 additions & 0 deletions tests/test_scoping.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def test_set_get_ids_scoping_int64_array(server_type):
assert np.allclose(scop.ids, ids_list)


def test_set_get_ids_scoping_raise_dtype_array(server_type):
scop = Scoping(server=server_type)
ids_list = [1.0, 2.0, 3.0, 4.0]
ids = np.array(ids_list)
with pytest.raises(ValueError, match="Accepted dtypes"):
scop.ids = ids


def test_set_get_ids_scoping_range(server_type):
range_ids = range(1, 10)
scop = Scoping(
Expand Down

0 comments on commit d03ce5e

Please sign in to comment.