From 22e80ed634133cca807774d8048c83eea53b46b8 Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Date: Sat, 19 Jul 2025 06:27:54 +0530 Subject: [PATCH 1/3] Fixing check_zero_fill_value Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --- sparse/numba_backend/_utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sparse/numba_backend/_utils.py b/sparse/numba_backend/_utils.py index 6fb1479b..a6360a5b 100644 --- a/sparse/numba_backend/_utils.py +++ b/sparse/numba_backend/_utils.py @@ -588,6 +588,8 @@ def check_zero_fill_value(*args, loose=True): ValueError: This operation requires zero fill values, but argument 1 had a fill value of 0.5. """ for i, arg in enumerate(args): + if arg.size == 0: + continue if hasattr(arg, "fill_value") and not equivalent(arg.fill_value, _zero_of_dtype(arg.dtype), loose=loose): raise ValueError( f"This operation requires zero fill values, but argument {i:d} had a fill value of {arg.fill_value!s}." From a78104176324d214e8dbd1c64975f6a0522efb0e Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Date: Sat, 19 Jul 2025 13:04:18 +0530 Subject: [PATCH 2/3] Adding test for check_zero_fill_value Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --- sparse/numba_backend/tests/test_coo.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sparse/numba_backend/tests/test_coo.py b/sparse/numba_backend/tests/test_coo.py index 767ba83d..e4a6b5e2 100644 --- a/sparse/numba_backend/tests/test_coo.py +++ b/sparse/numba_backend/tests/test_coo.py @@ -6,7 +6,7 @@ import sparse from sparse import COO, DOK from sparse.numba_backend._settings import NEP18_ENABLED -from sparse.numba_backend._utils import assert_eq, html_table, random_value_array +from sparse.numba_backend._utils import assert_eq, check_zero_fill_value, html_table, random_value_array import pytest @@ -1918,6 +1918,14 @@ def test_to_invalid_device(): s.to_device("invalid_device") +def test_check_zero_fill_value(): + a = sparse.COO(coords=np.empty((2, 0), dtype=int), data=np.array([]), shape=(1, 0), fill_value=1) + check_zero_fill_value(a) # This should not raise an error + with pytest.raises(ValueError, match="This operation requires zero fill values"): + s1 = sparse.random((10,), density=0.5, fill_value=1.0) + check_zero_fill_value(s1) + + # regression test for gh-869 def test_xH_x(): Y = np.array([[0, -1j], [+1j, 0]]) From 13ed355ccf8a0ff67f53bd84d62cf9c65718d1a2 Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Date: Sat, 19 Jul 2025 16:26:44 +0530 Subject: [PATCH 3/3] Adding test from issue Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --- sparse/numba_backend/tests/test_coo.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sparse/numba_backend/tests/test_coo.py b/sparse/numba_backend/tests/test_coo.py index e4a6b5e2..7b6dfd83 100644 --- a/sparse/numba_backend/tests/test_coo.py +++ b/sparse/numba_backend/tests/test_coo.py @@ -1918,9 +1918,11 @@ def test_to_invalid_device(): s.to_device("invalid_device") +# regression test for gh-877 def test_check_zero_fill_value(): - a = sparse.COO(coords=np.empty((2, 0), dtype=int), data=np.array([]), shape=(1, 0), fill_value=1) - check_zero_fill_value(a) # This should not raise an error + a = sparse.zeros((1, 0)) + b = a - sparse.mean(a, axis=1) + b @ b.T # should not raise an error with pytest.raises(ValueError, match="This operation requires zero fill values"): s1 = sparse.random((10,), density=0.5, fill_value=1.0) check_zero_fill_value(s1)