From 4d4fca0cef2e5594a50c1d10a9db6f21aaba0a58 Mon Sep 17 00:00:00 2001 From: "M. Eric Irrgang" Date: Tue, 13 Sep 2022 09:20:45 +0300 Subject: [PATCH] TST: Move new `asarray` test to a more appropriate place. (#22251) As noted at #21995 (comment), the new test from #21995 was placed in a directory intended for the Array API, and unrelated to the change. * Consolidate test_dtype_identity into an existing test file. Remove `test_asarray.py`. Create a new `TestAsArray` suite in `test_array_coercion.py` * Linting. Wrap some comments that got too long after function became a method (with additional indentation). --- numpy/array_api/tests/test_asarray.py | 64 ---------------------- numpy/core/tests/test_array_coercion.py | 70 ++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 66 deletions(-) delete mode 100644 numpy/array_api/tests/test_asarray.py diff --git a/numpy/array_api/tests/test_asarray.py b/numpy/array_api/tests/test_asarray.py deleted file mode 100644 index 5c269823f94b..000000000000 --- a/numpy/array_api/tests/test_asarray.py +++ /dev/null @@ -1,64 +0,0 @@ -import itertools - -import numpy as np - - -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}) - 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 - # 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) - 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 diff --git a/numpy/core/tests/test_array_coercion.py b/numpy/core/tests/test_array_coercion.py index 6abe942bd480..3a074a2b5c17 100644 --- a/numpy/core/tests/test_array_coercion.py +++ b/numpy/core/tests/test_array_coercion.py @@ -4,11 +4,11 @@ are tested (sometimes indirectly) elsewhere. """ +from itertools import permutations, product + import pytest from pytest import param -from itertools import product - import numpy as np from numpy.core._rational_tests import rational from numpy.core._multiarray_umath import _discover_array_parameters @@ -749,6 +749,72 @@ def __getitem__(self): np.array(BadSequence()) +class TestAsArray: + """Test expected behaviors of ``asarray``.""" + + def test_dtype_identity(self): + """Confirm the intended behavior for *dtype* kwarg. + + 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}) + 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 + # 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) + 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 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 + + class TestSpecialAttributeLookupFailure: # An exception was raised while fetching the attribute