"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "numpy/core/tests/test_casting_unittests.py" between
numpy-1.21.0.tar.gz and numpy-1.21.1.tar.gz

About: NumPy is the fundamental package for scientific computing with Python.

test_casting_unittests.py  (numpy-1.21.0):test_casting_unittests.py  (numpy-1.21.1)
skipping to change at line 144 skipping to change at line 144
# But in general, we do consider these safe: # But in general, we do consider these safe:
assert np.can_cast("d", "V") assert np.can_cast("d", "V")
assert np.can_cast("S20", "V") assert np.can_cast("S20", "V")
# Do not consider it a safe cast if the void is too smaller: # Do not consider it a safe cast if the void is too smaller:
assert not np.can_cast("d", "V1") assert not np.can_cast("d", "V1")
assert not np.can_cast("S20", "V1") assert not np.can_cast("S20", "V1")
assert not np.can_cast("U1", "V1") assert not np.can_cast("U1", "V1")
# Structured to unstructured is just like any other: # Structured to unstructured is just like any other:
assert np.can_cast("d,i", "V", casting="same_kind") assert np.can_cast("d,i", "V", casting="same_kind")
# Unstructured void to unstructured is actually no cast at all:
assert np.can_cast("V3", "V", casting="no")
assert np.can_cast("V0", "V", casting="no")
class TestCasting: class TestCasting:
size = 1500 # Best larger than NPY_LOWLEVEL_BUFFER_BLOCKSIZE * itemsize size = 1500 # Best larger than NPY_LOWLEVEL_BUFFER_BLOCKSIZE * itemsize
def get_data(self, dtype1, dtype2): def get_data(self, dtype1, dtype2):
if dtype2 is None or dtype1.itemsize >= dtype2.itemsize: if dtype2 is None or dtype1.itemsize >= dtype2.itemsize:
length = self.size // dtype1.itemsize length = self.size // dtype1.itemsize
else: else:
length = self.size // dtype2.itemsize length = self.size // dtype2.itemsize
skipping to change at line 641 skipping to change at line 644
def test_object_to_parametric_internal_error(self): def test_object_to_parametric_internal_error(self):
# We reject casting from object to a parametric type, without # We reject casting from object to a parametric type, without
# figuring out the correct instance first. # figuring out the correct instance first.
object_dtype = type(np.dtype(object)) object_dtype = type(np.dtype(object))
other_dtype = type(np.dtype(str)) other_dtype = type(np.dtype(str))
cast = get_castingimpl(object_dtype, other_dtype) cast = get_castingimpl(object_dtype, other_dtype)
with pytest.raises(TypeError, with pytest.raises(TypeError,
match="casting from object to the parametric DType"): match="casting from object to the parametric DType"):
cast._resolve_descriptors((np.dtype("O"), None)) cast._resolve_descriptors((np.dtype("O"), None))
@pytest.mark.parametrize("casting", ["no", "unsafe"])
def test_void_and_structured_with_subarray(self, casting):
# test case corresponding to gh-19325
dtype = np.dtype([("foo", "<f4", (3, 2))])
expected = casting == "unsafe"
assert np.can_cast("V4", dtype, casting=casting) == expected
assert np.can_cast(dtype, "V4", casting=casting) == expected
@pytest.mark.parametrize("dtype", np.typecodes["All"])
def test_object_casts_NULL_None_equivalence(self, dtype):
# None to <other> casts may succeed or fail, but a NULL'ed array must
# behave the same as one filled with None's.
arr_normal = np.array([None] * 5)
arr_NULLs = np.empty_like([None] * 5)
# If the check fails (maybe it should) the test would lose its purpose:
assert arr_NULLs.tobytes() == b"\x00" * arr_NULLs.nbytes
try:
expected = arr_normal.astype(dtype)
except TypeError:
with pytest.raises(TypeError):
arr_NULLs.astype(dtype)
else:
assert_array_equal(expected, arr_NULLs.astype(dtype))
 End of changes. 2 change blocks. 
0 lines changed or deleted 3 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)