From 7c700e70c5dd9148b869556621d27522c8affc55 Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Mon, 18 Nov 2024 15:21:15 +0100 Subject: [PATCH 1/3] ENH: generate numpy scalars or 0D arrays --- array_api_tests/hypothesis_helpers.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/array_api_tests/hypothesis_helpers.py b/array_api_tests/hypothesis_helpers.py index 950b6d4c..d9fdabd5 100644 --- a/array_api_tests/hypothesis_helpers.py +++ b/array_api_tests/hypothesis_helpers.py @@ -64,7 +64,7 @@ def from_dtype(dtype, **kwargs) -> SearchStrategy[Scalar]: @wraps(xps.arrays) -def arrays(dtype, *args, elements=None, **kwargs) -> SearchStrategy[Array]: +def arrays_no_scalars(dtype, *args, elements=None, **kwargs) -> SearchStrategy[Array]: """xps.arrays() without the crazy large numbers.""" if isinstance(dtype, SearchStrategy): return dtype.flatmap(lambda d: arrays(d, *args, elements=elements, **kwargs)) @@ -77,6 +77,19 @@ def arrays(dtype, *args, elements=None, **kwargs) -> SearchStrategy[Array]: return xps.arrays(dtype, *args, elements=elements, **kwargs) +def _f(a, flag): + return a[()] if a.ndim==0 and flag else a + + +@wraps(xps.arrays) +def arrays(dtype, *args, elements=None, **kwargs) -> SearchStrategy[Array]: + """xps.arrays() without the crazy large numbers. Also draw 0D arrays or numpy scalars. + + Is only relevant for numpy: on all other libraries, array[()] is no-op. + """ + return builds(_f, arrays_no_scalars(dtype, *args, elements=elements, **kwargs), booleans()) + + _dtype_categories = [(xp.bool,), dh.uint_dtypes, dh.int_dtypes, dh.real_float_dtypes, dh.complex_dtypes] _sorted_dtypes = [d for category in _dtype_categories for d in category] From 0f59c216262f23f33e1d59968b19d71899c1d855 Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Wed, 20 Nov 2024 15:07:37 +0200 Subject: [PATCH 2/3] BUG: avoid generating numpy scalars in test_asarray_arrays Otherwise, on numpy we can generate a numpy scalar and copy=False, which fails with (Pdb) p np.asarray(np.False_, copy=False) *** ValueError: Unable to avoid copy while creating an array as requested. --- array_api_tests/test_creation_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/array_api_tests/test_creation_functions.py b/array_api_tests/test_creation_functions.py index 60014b74..ec9bd188 100644 --- a/array_api_tests/test_creation_functions.py +++ b/array_api_tests/test_creation_functions.py @@ -263,7 +263,7 @@ def scalar_eq(s1: Scalar, s2: Scalar) -> bool: data=st.data(), ) def test_asarray_arrays(shape, dtypes, data): - x = data.draw(hh.arrays(dtype=dtypes.input_dtype, shape=shape), label="x") + x = data.draw(hh.arrays_no_scalars(dtype=dtypes.input_dtype, shape=shape), label="x") dtypes_strat = st.just(dtypes.input_dtype) if dtypes.input_dtype == dtypes.result_dtype: dtypes_strat |= st.none() From 024956ac230886b1fd6a239e89e5971da2d76b7f Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Wed, 20 Nov 2024 22:14:34 +0200 Subject: [PATCH 3/3] add a code comment --- array_api_tests/test_creation_functions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/array_api_tests/test_creation_functions.py b/array_api_tests/test_creation_functions.py index ec9bd188..8c504a2a 100644 --- a/array_api_tests/test_creation_functions.py +++ b/array_api_tests/test_creation_functions.py @@ -263,6 +263,7 @@ def scalar_eq(s1: Scalar, s2: Scalar) -> bool: data=st.data(), ) def test_asarray_arrays(shape, dtypes, data): + # generate arrays only since we draw the copy= kwd below (and np.asarray(scalar, copy=False) error out) x = data.draw(hh.arrays_no_scalars(dtype=dtypes.input_dtype, shape=shape), label="x") dtypes_strat = st.just(dtypes.input_dtype) if dtypes.input_dtype == dtypes.result_dtype: