Repository: numpy/numpy
File: numpy/testing/_private/utils.py (Python)

Code before:
        Combine results of running func on x and y, checking that they are True
        at the same locations.

        """
        __tracebackhide__ = True  # Hide traceback for py.test

        x_id = func(x)
        y_id = func(y)
        # We include work-arounds here to handle three types of slightly
        # pathological ndarray subclasses:
        # (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 in
        #     that case.
        # (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 cases (2) and (3), but it's nice to
        # support them if possible.
        result = x_id == y_id
        if isinstance(result, bool):
            result = np.bool(result)
        if result.all() != True:
            msg = build_err_msg(
                [x, y],
                err_msg + '\n%s location mismatch:'
                % (hasval), verbose=verbose, header=header,
                names=names,
                precision=precision)
            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:
            return np.bool(y_id)
        else:
            return y_id

    try:
        if strict:
            cond = x.shape == y.shape and x.dtype == y.dtype
        else:
            cond = (x.shape == () or y.shape == ()) or x.shape == y.shape
        if not cond:
            if x.shape != y.shape:
                reason = f'\n(shapes {x.shape}, {y.shape} mismatch)'
            else:

Review comment (suggestion):
Playing devil's advocate: what if someone passes something into this routine that returns something that's not a bool from `==` but that also doesn't have an `all()` method. You've introduced a new `AttributeError` that the cast to `np.bool` used to guard against.

Code after:
        """Handling nan/inf.

        Combine results of running func on x and y, checking that they are True
        at the same locations.

        """
        __tracebackhide__ = True  # Hide traceback for py.test

        x_id = func(x)
        y_id = func(y)
        # We include work-arounds here to handle three types of slightly
        # pathological ndarray subclasses:
        # (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() 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 cases (2) and (3), but it's nice to
        # support them if possible.
        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:'
                % (hasval), verbose=verbose, header=header,
                names=names,
                precision=precision)
            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:
            return np.bool(y_id)
        else:
            return y_id

    try:
        if strict:
            cond = x.shape == y.shape and x.dtype == y.dtype
        else:
            cond = (x.shape == () or y.shape == ()) or x.shape == y.shape
        if not cond: