From ab0c574a8207fe39fbfbf673e9818b0ee138337e Mon Sep 17 00:00:00 2001 From: Knut Rand Date: Wed, 6 Mar 2024 08:57:29 +0100 Subject: [PATCH 1/2] test: Issue #15 --- tests/test_raggedarray_usage.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/test_raggedarray_usage.py diff --git a/tests/test_raggedarray_usage.py b/tests/test_raggedarray_usage.py new file mode 100644 index 00000000..90a8f4b6 --- /dev/null +++ b/tests/test_raggedarray_usage.py @@ -0,0 +1,11 @@ +import pytest + +from npstructures import RaggedArray + +# this works +@pytest.mark.xfail +def test_assert_raises_value_error(): + with pytest.raises(ValueError): + x = RaggedArray([0, 1, 2, 3], [4, 5, 6]) + + From f5e075fb09ecab0f1abc5fbe95078be75a829d92 Mon Sep 17 00:00:00 2001 From: Knut Rand Date: Wed, 6 Mar 2024 09:23:46 +0100 Subject: [PATCH 2/2] fix: Issue #15 --- npstructures/raggedarray/__init__.py | 2 ++ tests/test_raggedarray_usage.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/npstructures/raggedarray/__init__.py b/npstructures/raggedarray/__init__.py index 8ba6a929..be177e5f 100644 --- a/npstructures/raggedarray/__init__.py +++ b/npstructures/raggedarray/__init__.py @@ -96,6 +96,8 @@ def __init__(self, data: npt.ArrayLike, shape: ShapeLike = None, dtype: npt.DTyp shape = shape else: shape = RaggedShape.asshape(shape) + if not shape.size == len(data) and safe_mode: + raise ValueError(f"The total size of provided shape {shape.size} does not match the size of the data array: {len(data)}") if not hasattr(data, "__array_ufunc__"): data = np.asanyarray(data, dtype=dtype) super().__init__(data, shape) diff --git a/tests/test_raggedarray_usage.py b/tests/test_raggedarray_usage.py index 90a8f4b6..bb2f95da 100644 --- a/tests/test_raggedarray_usage.py +++ b/tests/test_raggedarray_usage.py @@ -3,7 +3,7 @@ from npstructures import RaggedArray # this works -@pytest.mark.xfail + def test_assert_raises_value_error(): with pytest.raises(ValueError): x = RaggedArray([0, 1, 2, 3], [4, 5, 6])