diff --git a/src/ansys/dpf/core/scoping.py b/src/ansys/dpf/core/scoping.py index 9c455fbfa8..cfb49779e2 100644 --- a/src/ansys/dpf/core/scoping.py +++ b/src/ansys/dpf/core/scoping.py @@ -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: @@ -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)) diff --git a/tests/test_scoping.py b/tests/test_scoping.py index 2e01738682..62f646dd94 100644 --- a/tests/test_scoping.py +++ b/tests/test_scoping.py @@ -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(