def test_rational_dtype(): # test for bug gh-5719 a = np.array([1111], dtype=rational).astype assert_raises(OverflowError, a, "int8") # test that dtype detection finds user-defined types x = rational(1) assert_equal(np.array([x, x]).dtype, np.dtype(rational))
def test_array_impossible_casts(array): # All builtin types can forst cast as least theoretically # but user dtypes cannot necessarily. rt = rational(1, 2) if array: rt = np.array(rt) with assert_raises(ValueError): np.array(rt, dtype="M8")
def test_rational_dtype(): # test for bug gh-5719 a = np.array([1111], dtype=rational).astype assert_raises(OverflowError, a, 'int8') # test that dtype detection finds user-defined types x = rational(1) assert_equal(np.array([x,x]).dtype, np.dtype(rational))
def test_array_impossible_casts(array): # All builtin types can be forcibly cast, at least theoretically, # but user dtypes cannot necessarily. rt = rational(1, 2) if array: rt = np.array(rt) with assert_raises(TypeError): np.array(rt, dtype="M8")
def test_user_scalar_fails_buffer(self): r = rational(1) with assert_raises(TypeError): memoryview(r) # Check that we do not allow writeable buffer export with pytest.raises(BufferError, match="scalar buffer is readonly"): get_buffer_info(r, ["WRITABLE"])
def scalar_instances(times=True, extended_precision=True, user_dtype=True): # Hard-coded list of scalar instances. # Floats: yield param(np.sqrt(np.float16(5)), id="float16") yield param(np.sqrt(np.float32(5)), id="float32") yield param(np.sqrt(np.float64(5)), id="float64") if extended_precision: yield param(np.sqrt(np.longdouble(5)), id="longdouble") # Complex: yield param(np.sqrt(np.complex64(2 + 3j)), id="complex64") yield param(np.sqrt(np.complex128(2 + 3j)), id="complex128") if extended_precision: yield param(np.sqrt(np.longcomplex(2 + 3j)), id="clongdouble") # Bool: # XFAIL: Bool should be added, but has some bad properties when it # comes to strings, see also gh-9875 # yield param(np.bool_(0), id="bool") # Integers: yield param(np.int8(2), id="int8") yield param(np.int16(2), id="int16") yield param(np.int32(2), id="int32") yield param(np.int64(2), id="int64") yield param(np.uint8(2), id="uint8") yield param(np.uint16(2), id="uint16") yield param(np.uint32(2), id="uint32") yield param(np.uint64(2), id="uint64") # Rational: if user_dtype: yield param(rational(1, 2), id="rational") # Cannot create a structured void scalar directly: structured = np.array([(1, 3)], "i,i")[0] assert isinstance(structured, np.void) assert structured.dtype == np.dtype("i,i") yield param(structured, id="structured") if times: # Datetimes and timedelta yield param(np.timedelta64(2), id="timedelta64[generic]") yield param(np.timedelta64(23, "s"), id="timedelta64[s]") yield param(np.timedelta64("NaT", "s"), id="timedelta64[s](NaT)") yield param(np.datetime64("NaT"), id="datetime64[generic](NaT)") yield param(np.datetime64("2020-06-07 12:43", "ms"), id="datetime64[ms]") # Strings and unstructured void: yield param(np.bytes_(b"1234"), id="bytes") yield param(np.unicode_("2345"), id="unicode") yield param(np.void(b"4321"), id="unstructured_void")
def test_as_strided(): a = np.array([None]) a_view = as_strided(a) expected = np.array([None]) assert_array_equal(a_view, np.array([None])) a = np.array([1, 2, 3, 4]) a_view = as_strided(a, shape=(2, ), strides=(2 * a.itemsize, )) expected = np.array([1, 3]) assert_array_equal(a_view, expected) a = np.array([1, 2, 3, 4]) a_view = as_strided(a, shape=(3, 4), strides=(0, 1 * a.itemsize)) expected = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]) assert_array_equal(a_view, expected) # Regression test for gh-5081 dt = np.dtype([('num', 'i4'), ('obj', 'O')]) a = np.empty((4, ), dtype=dt) a['num'] = np.arange(1, 5) a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize)) expected_num = [[1, 2, 3, 4]] * 3 expected_obj = [[None] * 4] * 3 assert_equal(a_view.dtype, dt) assert_array_equal(expected_num, a_view['num']) assert_array_equal(expected_obj, a_view['obj']) # Make sure that void types without fields are kept unchanged a = np.empty((4, ), dtype='V4') a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize)) assert_equal(a.dtype, a_view.dtype) # Make sure that the only type that could fail is properly handled dt = np.dtype({'names': [''], 'formats': ['V4']}) a = np.empty((4, ), dtype=dt) a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize)) assert_equal(a.dtype, a_view.dtype) # Custom dtypes should not be lost (gh-9161) r = [rational(i) for i in range(4)] a = np.array(r, dtype=rational) a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize)) assert_equal(a.dtype, a_view.dtype) assert_array_equal([r] * 3, a_view)
def test_as_strided(): a = np.array([None]) a_view = as_strided(a) expected = np.array([None]) assert_array_equal(a_view, np.array([None])) a = np.array([1, 2, 3, 4]) a_view = as_strided(a, shape=(2,), strides=(2 * a.itemsize,)) expected = np.array([1, 3]) assert_array_equal(a_view, expected) a = np.array([1, 2, 3, 4]) a_view = as_strided(a, shape=(3, 4), strides=(0, 1 * a.itemsize)) expected = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]) assert_array_equal(a_view, expected) # Regression test for gh-5081 dt = np.dtype([('num', 'i4'), ('obj', 'O')]) a = np.empty((4,), dtype=dt) a['num'] = np.arange(1, 5) a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize)) expected_num = [[1, 2, 3, 4]] * 3 expected_obj = [[None]*4]*3 assert_equal(a_view.dtype, dt) assert_array_equal(expected_num, a_view['num']) assert_array_equal(expected_obj, a_view['obj']) # Make sure that void types without fields are kept unchanged a = np.empty((4,), dtype='V4') a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize)) assert_equal(a.dtype, a_view.dtype) # Make sure that the only type that could fail is properly handled dt = np.dtype({'names': [''], 'formats': ['V4']}) a = np.empty((4,), dtype=dt) a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize)) assert_equal(a.dtype, a_view.dtype) # Custom dtypes should not be lost (gh-9161) r = [rational(i) for i in range(4)] a = np.array(r, dtype=rational) a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize)) assert_equal(a.dtype, a_view.dtype) assert_array_equal([r] * 3, a_view)
def test_third_party_scalar_type(self): from numpy.core._rational_tests import rational assert_raises(KeyError, np.sctype2char, rational) assert_raises(KeyError, np.sctype2char, rational(1))
def test_third_party_scalar_type(self): from numpy.core._rational_tests import rational assert_raises(KeyError, np.sctype2char, rational) assert_raises(KeyError, np.sctype2char, rational(1))