Skip to content

Commit 6285329

Browse files
committed
raise in places that have a kwarg casting, besides np.astype
1 parent 8e6039d commit 6285329

File tree

9 files changed

+46
-12
lines changed

9 files changed

+46
-12
lines changed

TODO_same_value

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
- Check where PyArray_CopyObject, PyArray_NewCopy, PyArray_CopyInto, array_datetime_as_string, PyArray_Concatenate, PyArray_where are used, do we need a 'same_value' equivalents?
2-
- In PyArray_FromArray(arr, newtype, flags) should there be a SAME_VALUE flag?
3-
- Examine places where PyArray_CastingConverter is used and add SAME_VALUE handling
4-
- array_datetime_as_string:
5-
- array_copyto:
1+
- Finish PyArray_AssignArray
62
- PyArray_AssignArray with wheremask (called with a cast arg)
73
- PyArray_AssignRawScalar with/without wheremask
8-
- PyArray_ConcatenateInto (called with a cast arg)
9-
- PyArray_EinsteinSum (called with a cast arg)
10-
- NpyIter_AdvancedNew (called with a cast arg)
114
In CanCast, make sure user defined and datetime dtypes will fail with SAME_VALUE
125
----

numpy/_core/src/multiarray/array_assign_array.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ raw_array_wheremasked_assign_array(int ndim, npy_intp const *shape,
245245
&cast_info, &method_flags) != NPY_SUCCEED) {
246246
return -1;
247247
}
248+
if (same_value_cast) {
249+
cast_info.context.flags |= NPY_SAME_VALUE_CASTING;
250+
PyErr_SetString(PyExc_NotImplementedError,
251+
"raw_array_wheremasked_assign_array with 'same_value' casting not implemented yet");
252+
return -1;
253+
}
248254

249255
if (!(method_flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) {
250256
npy_clear_floatstatus_barrier(src_data);
@@ -256,9 +262,7 @@ raw_array_wheremasked_assign_array(int ndim, npy_intp const *shape,
256262
}
257263
NPY_BEGIN_THREADS_THRESHOLDED(nitems);
258264
}
259-
if (same_value_cast) {
260-
cast_info.context.flags |= NPY_SAME_VALUE_CASTING;
261-
}
265+
262266
npy_intp strides[2] = {src_strides_it[0], dst_strides_it[0]};
263267

264268
NPY_RAW_ITER_START(idim, ndim, coord, shape_it) {

numpy/_core/src/multiarray/datetime.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,6 @@ can_cast_datetime64_units(NPY_DATETIMEUNIT src_unit,
12361236
switch (casting) {
12371237
/* Allow anything with unsafe casting */
12381238
case NPY_UNSAFE_CASTING:
1239-
case NPY_SAME_VALUE_CASTING:
12401239
return 1;
12411240

12421241
/*
@@ -1262,6 +1261,8 @@ can_cast_datetime64_units(NPY_DATETIMEUNIT src_unit,
12621261
return (src_unit <= dst_unit);
12631262
}
12641263

1264+
case NPY_SAME_VALUE_CASTING:
1265+
return 0;
12651266
/* Enforce equality with 'no' or 'equiv' casting */
12661267
default:
12671268
return src_unit == dst_unit;

numpy/_core/src/multiarray/einsum.c.src

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,12 @@ PyArray_EinsteinSum(char *subscripts, npy_intp nop,
830830
return NULL;
831831
}
832832

833+
if (casting == NPY_SAME_VALUE_CASTING) {
834+
PyErr_SetString(PyExc_NotImplementedError,
835+
"einsum with casting='same_value' not implemented yet");
836+
return NULL;
837+
}
838+
833839
/* Parse the subscripts string into label_counts and op_labels */
834840
memset(label_counts, 0, sizeof(label_counts));
835841
for (iop = 0; iop < nop; ++iop) {

numpy/_core/src/multiarray/multiarraymodule.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,12 @@ PyArray_ConcatenateInto(PyObject *op,
666666
"argument, but both were provided.");
667667
return NULL;
668668
}
669+
if (casting == NPY_SAME_VALUE_CASTING) {
670+
PyErr_SetString(PyExc_NotImplementedError,
671+
"concatenate with casting='same_value' not implemented yet");
672+
return NULL;
673+
}
674+
669675

670676
/* Convert the input list into arrays */
671677
narrays = PySequence_Size(op);
@@ -1940,6 +1946,11 @@ array_copyto(PyObject *NPY_UNUSED(ignored),
19401946
NULL, NULL, NULL) < 0) {
19411947
goto fail;
19421948
}
1949+
if (casting == NPY_SAME_VALUE_CASTING) {
1950+
PyErr_SetString(PyExc_NotImplementedError,
1951+
"raw_array_wheremasked_assign_array with 'same_value' casting not implemented yet");
1952+
goto fail;
1953+
}
19431954

19441955
if (!PyArray_Check(dst_obj)) {
19451956
PyErr_Format(PyExc_TypeError,

numpy/_core/src/umath/ufunc_object.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4538,6 +4538,10 @@ ufunc_generic_fastcall(PyUFuncObject *ufunc,
45384538
full_args.in, casting) < 0) {
45394539
goto fail;
45404540
}
4541+
if (casting == NPY_SAME_VALUE_CASTING) {
4542+
PyErr_SetString(PyExc_NotImplementedError,
4543+
"ufunc with 'same_value' casting not implemented yet");
4544+
}
45414545

45424546
/*
45434547
* Do the final preparations and call the inner-loop.

numpy/_core/tests/test_datetime.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,10 @@ def test_datetime_as_string(self):
18401840
'2032-07-18')
18411841
assert_equal(np.datetime_as_string(a, unit='D', casting='unsafe'),
18421842
'2032-07-18')
1843+
1844+
with pytest.raises(TypeError):
1845+
np.datetime_as_string(a, unit='Y', casting='same_value')
1846+
18431847
assert_equal(np.datetime_as_string(a, unit='h'), '2032-07-18T12')
18441848
assert_equal(np.datetime_as_string(a, unit='m'),
18451849
'2032-07-18T12:23')
@@ -1886,6 +1890,7 @@ def test_datetime_as_string(self):
18861890
np.datetime64('2032-01-01T00:00:00', 'us'), unit='auto'),
18871891
'2032-01-01')
18881892

1893+
18891894
@pytest.mark.skipif(not _has_pytz, reason="The pytz module is not available.")
18901895
def test_datetime_as_string_timezone(self):
18911896
# timezone='local' vs 'UTC'

numpy/_core/tests/test_einsum.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ def test_einsum_errors(self, do_opt, einsum_fn):
7979
b = np.ones((3, 4, 5))
8080
einsum_fn('aabcb,abc', a, b)
8181

82+
with pytest.raises(NotImplementedError):
83+
a = np.arange(3)
84+
# einsum_path does not accept kwarg 'casting'
85+
np.einsum('ij->j', [a, a], casting='same_value')
86+
8287
def test_einsum_sorting_behavior(self):
8388
# Case 1: 26 dimensions (all lowercase indices)
8489
n1 = 26

numpy/_core/tests/test_shape_base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,11 @@ def test_concatenate(self):
365365
assert_(out is rout)
366366
assert_equal(res, rout)
367367

368+
def test_concatenate_same_value(self):
369+
r4 = list(range(4))
370+
with pytest.raises(NotImplementedError):
371+
concatenate([r4, r4], casting="same_value")
372+
368373
@pytest.mark.skipif(IS_PYPY, reason="PYPY handles sq_concat, nb_add differently than cpython")
369374
def test_operator_concat(self):
370375
import operator

0 commit comments

Comments
 (0)