From 81b97607339ac68b27cf72ba7923345d58e2895e Mon Sep 17 00:00:00 2001 From: "M. Eric Irrgang" Date: Sat, 16 Jul 2022 15:27:38 -0500 Subject: Add unit testing. --- numpy/array_api/tests/test_asarray.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 numpy/array_api/tests/test_asarray.py (limited to 'numpy/array_api/tests') diff --git a/numpy/array_api/tests/test_asarray.py b/numpy/array_api/tests/test_asarray.py new file mode 100644 index 000000000..d9bbc675b --- /dev/null +++ b/numpy/array_api/tests/test_asarray.py @@ -0,0 +1,24 @@ +import numpy as np + + +def test_fast_return(): + """""" + a = np.array([1, 2, 3], dtype='i') + assert np.asarray(a) is a + assert np.asarray(a, dtype='i') is a + # This may produce a new view or a copy, but is never the same object. + assert np.asarray(a, dtype='l') is not a + + unequal_type = np.dtype('i', metadata={'spam': True}) + b = np.asarray(a, dtype=unequal_type) + assert b is not a + assert b.base is a + + equivalent_requirement = np.dtype('i', metadata={'spam': True}) + c = np.asarray(b, dtype=equivalent_requirement) + # A quirk of the metadata test is that equivalent metadata dicts are still + # separate objects and so don't evaluate as the same array type description. + assert unequal_type == equivalent_requirement + assert unequal_type is not equivalent_requirement + assert c is not b + assert c.dtype is equivalent_requirement -- cgit v1.2.1 From 5651445944bce163a2c3f746d6ac1acd9ae76032 Mon Sep 17 00:00:00 2001 From: "M. Eric Irrgang" Date: Sat, 16 Jul 2022 15:51:51 -0500 Subject: Update comment and obey formatting requirements. --- numpy/array_api/tests/test_asarray.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy/array_api/tests') diff --git a/numpy/array_api/tests/test_asarray.py b/numpy/array_api/tests/test_asarray.py index d9bbc675b..bd1859d71 100644 --- a/numpy/array_api/tests/test_asarray.py +++ b/numpy/array_api/tests/test_asarray.py @@ -16,8 +16,8 @@ def test_fast_return(): equivalent_requirement = np.dtype('i', metadata={'spam': True}) c = np.asarray(b, dtype=equivalent_requirement) - # A quirk of the metadata test is that equivalent metadata dicts are still - # separate objects and so don't evaluate as the same array type description. + # The descriptors are equivalent, but we have created + # distinct dtype instances. assert unequal_type == equivalent_requirement assert unequal_type is not equivalent_requirement assert c is not b -- cgit v1.2.1 From e286f461b54c43e2a16b3f6fc6d829936ea28c27 Mon Sep 17 00:00:00 2001 From: "M. Eric Irrgang" Date: Sun, 17 Jul 2022 10:49:56 -0500 Subject: Expand test_asarray.py. * Improve comments/docs. * Improve descriptiveness of variable names. * Add additional test expressions that would not pass without this patch. --- numpy/array_api/tests/test_asarray.py | 46 +++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 13 deletions(-) (limited to 'numpy/array_api/tests') diff --git a/numpy/array_api/tests/test_asarray.py b/numpy/array_api/tests/test_asarray.py index bd1859d71..fdcc77506 100644 --- a/numpy/array_api/tests/test_asarray.py +++ b/numpy/array_api/tests/test_asarray.py @@ -1,24 +1,44 @@ import numpy as np -def test_fast_return(): - """""" - a = np.array([1, 2, 3], dtype='i') - assert np.asarray(a) is a - assert np.asarray(a, dtype='i') is a - # This may produce a new view or a copy, but is never the same object. - assert np.asarray(a, dtype='l') is not a +def test_dtype_identity(): + """Confirm the intended behavior for ``asarray`` results. + The result of ``asarray()`` should have the dtype provided through the + keyword argument, when used. This forces unique array handles to be + produced for unique np.dtype objects, but (for equivalent dtypes), the + underlying data (the base object) is shared with the original array object. + + Ref https://github.com/numpy/numpy/issues/1468 + """ + int_array = np.array([1, 2, 3], dtype='i') + assert np.asarray(int_array) is int_array + + # The character code resolves to the singleton dtype object provided + # by the numpy package. + assert np.asarray(int_array, dtype='i') is int_array + + # Derive a dtype from n.dtype('i'), but add a metadata object to force + # the dtype to be distinct. unequal_type = np.dtype('i', metadata={'spam': True}) - b = np.asarray(a, dtype=unequal_type) - assert b is not a - assert b.base is a + annotated_int_array = np.asarray(int_array, dtype=unequal_type) + assert annotated_int_array is not int_array + assert annotated_int_array.base is int_array + + # These ``asarray()`` calls may produce a new view or a copy, + # but never the same object. + long_int_array = np.asarray(int_array, dtype='l') + assert long_int_array is not int_array + assert np.asarray(int_array, dtype='q') is not int_array + assert np.asarray(long_int_array, dtype='q') is not long_int_array + assert np.asarray(int_array, dtype='l') is not np.asarray(int_array, dtype='l') + assert np.asarray(int_array, dtype='l').base is np.asarray(int_array, dtype='l').base equivalent_requirement = np.dtype('i', metadata={'spam': True}) - c = np.asarray(b, dtype=equivalent_requirement) + annotated_int_array_alt = np.asarray(annotated_int_array, dtype=equivalent_requirement) # The descriptors are equivalent, but we have created # distinct dtype instances. assert unequal_type == equivalent_requirement assert unequal_type is not equivalent_requirement - assert c is not b - assert c.dtype is equivalent_requirement + assert annotated_int_array_alt is not annotated_int_array + assert annotated_int_array_alt.dtype is equivalent_requirement -- cgit v1.2.1 From 01438a848b029b4fb3d3509c7fd313bc0588bd38 Mon Sep 17 00:00:00 2001 From: "M. Eric Irrgang" Date: Sun, 17 Jul 2022 10:55:59 -0500 Subject: Lint. Shorten some lines. --- numpy/array_api/tests/test_asarray.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'numpy/array_api/tests') diff --git a/numpy/array_api/tests/test_asarray.py b/numpy/array_api/tests/test_asarray.py index fdcc77506..4a9dd77a0 100644 --- a/numpy/array_api/tests/test_asarray.py +++ b/numpy/array_api/tests/test_asarray.py @@ -31,11 +31,12 @@ def test_dtype_identity(): assert long_int_array is not int_array assert np.asarray(int_array, dtype='q') is not int_array assert np.asarray(long_int_array, dtype='q') is not long_int_array - assert np.asarray(int_array, dtype='l') is not np.asarray(int_array, dtype='l') - assert np.asarray(int_array, dtype='l').base is np.asarray(int_array, dtype='l').base + assert long_int_array is not np.asarray(int_array, dtype='l') + assert long_int_array.base is np.asarray(int_array, dtype='l').base equivalent_requirement = np.dtype('i', metadata={'spam': True}) - annotated_int_array_alt = np.asarray(annotated_int_array, dtype=equivalent_requirement) + annotated_int_array_alt = np.asarray(annotated_int_array, + dtype=equivalent_requirement) # The descriptors are equivalent, but we have created # distinct dtype instances. assert unequal_type == equivalent_requirement -- cgit v1.2.1 From b1a8ff8fa73b744416e12cdd4bb70594717b5336 Mon Sep 17 00:00:00 2001 From: "M. Eric Irrgang" Date: Sun, 17 Jul 2022 13:12:31 -0500 Subject: Add release note and further clarify tests. --- numpy/array_api/tests/test_asarray.py | 43 +++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'numpy/array_api/tests') diff --git a/numpy/array_api/tests/test_asarray.py b/numpy/array_api/tests/test_asarray.py index 4a9dd77a0..5c269823f 100644 --- a/numpy/array_api/tests/test_asarray.py +++ b/numpy/array_api/tests/test_asarray.py @@ -1,3 +1,5 @@ +import itertools + import numpy as np @@ -24,22 +26,39 @@ def test_dtype_identity(): annotated_int_array = np.asarray(int_array, dtype=unequal_type) assert annotated_int_array is not int_array assert annotated_int_array.base is int_array - - # These ``asarray()`` calls may produce a new view or a copy, - # but never the same object. - long_int_array = np.asarray(int_array, dtype='l') - assert long_int_array is not int_array - assert np.asarray(int_array, dtype='q') is not int_array - assert np.asarray(long_int_array, dtype='q') is not long_int_array - assert long_int_array is not np.asarray(int_array, dtype='l') - assert long_int_array.base is np.asarray(int_array, dtype='l').base - + # Create an equivalent descriptor with a new and distinct dtype instance. equivalent_requirement = np.dtype('i', metadata={'spam': True}) annotated_int_array_alt = np.asarray(annotated_int_array, dtype=equivalent_requirement) - # The descriptors are equivalent, but we have created - # distinct dtype instances. assert unequal_type == equivalent_requirement assert unequal_type is not equivalent_requirement assert annotated_int_array_alt is not annotated_int_array assert annotated_int_array_alt.dtype is equivalent_requirement + + # Check the same logic for a pair of C types whose equivalence may vary + # between computing environments. + # Find an equivalent pair. + integer_type_codes = ('i', 'l', 'q') + integer_dtypes = [np.dtype(code) for code in integer_type_codes] + typeA = None + typeB = None + for typeA, typeB in itertools.permutations(integer_dtypes, r=2): + if typeA == typeB: + assert typeA is not typeB + break + assert isinstance(typeA, np.dtype) and isinstance(typeB, np.dtype) + + # These ``asarray()`` calls may produce a new view or a copy, + # but never the same object. + long_int_array = np.asarray(int_array, dtype='l') + long_long_int_array = np.asarray(int_array, dtype='q') + assert long_int_array is not int_array + assert long_long_int_array is not int_array + assert np.asarray(long_int_array, dtype='q') is not long_int_array + array_a = np.asarray(int_array, dtype=typeA) + assert typeA == typeB + assert typeA is not typeB + assert array_a.dtype is typeA + assert array_a is not np.asarray(array_a, dtype=typeB) + assert np.asarray(array_a, dtype=typeB).dtype is typeB + assert array_a is np.asarray(array_a, dtype=typeB).base -- cgit v1.2.1