diff options
-rw-r--r-- | numpy/core/tests/test_array_coercion.py | 14 | ||||
-rw-r--r-- | numpy/core/tests/test_casting_unittests.py | 24 |
2 files changed, 38 insertions, 0 deletions
diff --git a/numpy/core/tests/test_array_coercion.py b/numpy/core/tests/test_array_coercion.py index 45c792ad2..076d8e43f 100644 --- a/numpy/core/tests/test_array_coercion.py +++ b/numpy/core/tests/test_array_coercion.py @@ -342,6 +342,20 @@ class TestScalarDiscovery: ass[()] = scalar assert_array_equal(ass, cast) + @pytest.mark.parametrize("pyscalar", [10, 10.32, 10.14j, 10**100]) + def test_pyscalar_subclasses(self, pyscalar): + """NumPy arrays are read/write which means that anything but invariant + behaviour is on thin ice. However, we currently are happy to discover + subclasses of Python float, int, complex the same as the base classes. + This should potentially be deprecated. + """ + class MyScalar(type(pyscalar)): + pass + + res = np.array(MyScalar(pyscalar)) + expected = np.array(pyscalar) + assert_array_equal(res, expected) + @pytest.mark.parametrize("dtype_char", np.typecodes["All"]) def test_default_dtype_instance(self, dtype_char): if dtype_char in "SU": diff --git a/numpy/core/tests/test_casting_unittests.py b/numpy/core/tests/test_casting_unittests.py index 8398b3cad..3f67f1832 100644 --- a/numpy/core/tests/test_casting_unittests.py +++ b/numpy/core/tests/test_casting_unittests.py @@ -650,6 +650,30 @@ class TestCasting: match="casting from object to the parametric DType"): cast._resolve_descriptors((np.dtype("O"), None)) + @pytest.mark.parametrize("dtype", simple_dtype_instances()) + def test_object_and_simple_resolution(self, dtype): + # Simple test to exercise the cast when no instance is specified + object_dtype = type(np.dtype(object)) + cast = get_castingimpl(object_dtype, type(dtype)) + + safety, (_, res_dt) = cast._resolve_descriptors((np.dtype("O"), dtype)) + assert safety == Casting.unsafe + assert res_dt is dtype + + safety, (_, res_dt) = cast._resolve_descriptors((np.dtype("O"), None)) + assert safety == Casting.unsafe + assert res_dt == dtype.newbyteorder("=") + + @pytest.mark.parametrize("dtype", simple_dtype_instances()) + def test_simple_to_object_resolution(self, dtype): + # Simple test to exercise the cast when no instance is specified + object_dtype = type(np.dtype(object)) + cast = get_castingimpl(type(dtype), object_dtype) + + safety, (_, res_dt) = cast._resolve_descriptors((dtype, None)) + assert safety == Casting.safe + assert res_dt is np.dtype("O") + @pytest.mark.parametrize("casting", ["no", "unsafe"]) def test_void_and_structured_with_subarray(self, casting): # test case corresponding to gh-19325 |