8000 API: Allow comparisons with and between any python integers by seberg · Pull Request #24915 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

API: Allow comparisons with and between any python integers #24915

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 8 commits into from
Oct 19, 2023
Prev Previous commit
Next Next commit
TST: Add explicit test for new out-of-bound int handling
  • Loading branch information
seberg committed Oct 18, 2023
commit b483533fbf836ba70711cc846c95633b1bade103
40 changes: 39 additions & 1 deletion numpy/_core/tests/test_nep50_promotions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import hypothesis
from hypothesis import strategies

from numpy.testing import IS_WASM
from numpy.testing import assert_array_equal, IS_WASM


@pytest.fixture(scope="module", autouse=True)
Expand Down Expand Up @@ -271,3 +271,41 @@ def test_expected_promotion(expected, dtypes, optional_dtypes, data):

res = np.result_type(*dtypes_sample)
assert res == expected


@pytest.mark.parametrize("sctype",
[np.int8, np.int16, np.int32, np.int64,
np.uint8, np.uint16, np.uint32, np.uint64])
@pytest.mark.parametrize("other_val",
[-2*100, -1, 0, 9, 10, 11, 2**63, 2*100])
@pytest.mark.parametrize("comp",
[operator.eq, operator.ne, operator.le, operator.lt,
operator.ge, operator.gt])
def test_integer_comparison(sctype, other_val, comp):
np._set_promotion_state("weak")

# Test that comparisons with integers (especially out-of-bound) ones
# works correctly.
val_obj = 10
val = sctype(val_obj)
# Check that the scalar behaves the same as the python int:
assert comp(10, other_val) == comp(val, other_val)
assert comp(val, other_val) == comp(10, other_val)
# Except for the result type:
assert type(comp(val, other_val)) is np.bool_

# Check that the integer array and object array behave the same:
val_obj = np.array([10, 10], dtype=object)
val = val_obj.astype(sctype)
assert_array_equal(comp(val_obj, other_val), comp(val, other_val))
assert_array_equal(comp(other_val, val_obj), comp(other_val, val))


@pytest.mark.parametrize("comp",
[np.equal, np.not_equal, np.less_equal, np.less,
np.greater_equal, np.greater])
def test_integer_integer_comparison(comp):
np._set_promotion_state("weak")

# Test that the NumPy comparison ufuncs work with large Python integers
assert comp(2**200, -2**200) == comp(2**200, -2**200, dtype=object)
0