Skip to content

BUG: Fix np.testing utils failing for masked scalar vs. scalar (#29317) #29318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions numpy/testing/_private/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,16 +771,20 @@ def func_assert_same_pos(x, y, func=isnan, hasval='nan'):
y_id = func(y)
# We include work-arounds here to handle three types of slightly
# pathological ndarray subclasses:
# (1) all() on `masked` array scalars can return masked arrays, so we
# use != True
# (1) all() on fully masked arrays returns np.ma.masked, so we use != True
# (np.ma.masked != True evaluates as np.ma.masked, which is falsy).
# (2) __eq__ on some ndarray subclasses returns Python booleans
# instead of element-wise comparisons, so we cast to np.bool() and
# use isinstance(..., bool) checks
# instead of element-wise comparisons, so we cast to np.bool() in
# that case (or in case __eq__ returns some other value with no
# all() method).
# (3) subclasses with bare-bones __array_function__ implementations may
# not implement np.all(), so favor using the .all() method
# We are not committed to supporting such subclasses, but it's nice to
# We are not committed to supporting cases (2) and (3), but it's nice to
# support them if possible.
if np.bool(x_id == y_id).all() != True:
result = x_id == y_id
if not hasattr(result, "all") or not callable(result.all):
result = np.bool(result)
if result.all() != True:
msg = build_err_msg(
[x, y],
err_msg + '\n%s location mismatch:'
Expand All @@ -790,6 +794,9 @@ def func_assert_same_pos(x, y, func=isnan, hasval='nan'):
raise AssertionError(msg)
# If there is a scalar, then here we know the array has the same
# flag as it everywhere, so we should return the scalar flag.
# np.ma.masked is also handled and converted to np.False_ (even if the other
# array has nans/infs etc.; that's OK given the handling later of fully-masked
# results).
if isinstance(x_id, bool) or x_id.ndim == 0:
return np.bool(x_id)
elif isinstance(y_id, bool) or y_id.ndim == 0:
Expand Down
34 changes: 34 additions & 0 deletions numpy/testing/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,40 @@ def test_masked_nan_inf(self):
self._test_equal(a, b)
self._test_equal(b, a)

# Also provides test cases for gh-11121
def test_masked_scalar(self):
# Test masked scalar vs. plain/masked scalar
for a_val, b_val, b_masked in itertools.product(
[3., np.nan, np.inf],
[3., 4., np.nan, np.inf, -np.inf],
[False, True],
):
a = np.ma.MaskedArray(a_val, mask=True)
b = np.ma.MaskedArray(b_val, mask=True) if b_masked else np.array(b_val)
self._test_equal(a, b)
self._test_equal(b, a)

# Test masked scalar vs. plain array
for a_val, b_val in itertools.product(
[3., np.nan, -np.inf],
itertools.product([3., 4., np.nan, np.inf, -np.inf], repeat=2),
):
a = np.ma.MaskedArray(a_val, mask=True)
b = np.array(b_val)
self._test_equal(a, b)
self._test_equal(b, a)

# Test masked scalar vs. masked array
for a_val, b_val, b_mask in itertools.product(
[3., np.nan, np.inf],
itertools.product([3., 4., np.nan, np.inf, -np.inf], repeat=2),
itertools.product([False, True], repeat=2),
):
a = np.ma.MaskedArray(a_val, mask=True)
b = np.ma.MaskedArray(b_val, mask=b_mask)
self._test_equal(a, b)
self._test_equal(b, a)

def test_subclass_that_overrides_eq(self):
# While we cannot guarantee testing functions will always work for
# subclasses, the tests should ideally rely only on subclasses having
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy