BUG: np.take out dtype - #30615
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
792839e to
26d014a
Compare
|
I rebased my branch onto the latest main, and all CI/CD checks are now passing |
| raise AssertionError("compress with an out which cannot be " | ||
| "safely casted should not return " | ||
| "successfully") | ||
| assert_equal(b, np.array([[1.0], [3.0]])) |
There was a problem hiding this comment.
Can you please explain this change?
There was a problem hiding this comment.
The compress method uses PyArray_TakeFrom internally. Since it no longer raises an error after my changes, I have updated the test to verify that the output values match the expected results.
There was a problem hiding this comment.
OK, I guess this is fine to generalize. We may want to tighten the casting from same-kind at some point, but I don't think we have to worry about it now.
| assert_array_equal(a[indices], out) | ||
| diffrent_dtype_out = np.zeros_like(indices, dtype=np.uint32) | ||
| with pytest.warns(DeprecationWarning): | ||
| np.take(a, indices, out=diffrent_dtype_out) |
There was a problem hiding this comment.
Please move this test to test_deprecations and use the pattern used there.
There was a problem hiding this comment.
Fixed in commit 0029b81. Could you please check if the implementation and coding style are correct?
| "Implicit casting of output to a different kind is " | ||
| "deprecated. " | ||
| "In a future version, this will result in an error. Please " | ||
| "ensure the output has the same-kind type as the input.") < |
There was a problem hiding this comment.
Please add the comments we usually add to say when the deprecation happened (before it) and also inside the deprecation itself, such as (deprecated NumPy 2.5).
The last sentence feels like unnecessary to me. (I should think once more if we shouldn't just use safe casting, although then one might be tempted to ask for a casting= kwarg.)
There was a problem hiding this comment.
Fixed in commit 0029b81. I removed the last sentence.
| @@ -311,7 +311,25 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis, | |||
| } | |||
| dtype = PyArray_DESCR(self); | |||
| Py_INCREF(dtype); | |||
There was a problem hiding this comment.
This needs cleaning up, you are inserting code but that code interacts closely with this so you can't insert code between these two lines.
I.e. the dtype reference can be lost on error.
There was a problem hiding this comment.
Fixed in 0029b81. Is it safe to insert code between dtype = PyArray_DESCR(self); and Py_INCREF(dtype);? Also, is the line Py_INCREF(out_dtype); unnecessary or redundant here?
| } | ||
| } | ||
| flags |= NPY_ARRAY_FORCECAST; | ||
| obj = (PyArrayObject *)PyArray_FromArray(out, dtype, flags); |
There was a problem hiding this comment.
This is the exact same code as the first branch, except for a flag that is irrelevant in the first branch.
|
Thank you for the detailed feedback. I have pushed the fixes in the latest commit. |
seberg
left a comment
There was a problem hiding this comment.
Thanks a few nits, mainly the refcount addition is incorrect and I think it would be good to add a test that goes across kind boundaries (if you looked at it the other way).
| flags |= NPY_ARRAY_FORCECAST; | ||
| } | ||
| Py_INCREF(dtype); | ||
| Py_INCREF(out_dtype); |
There was a problem hiding this comment.
This incref isn't necessary here.
| if (DEPRECATE( | ||
| "Implicit casting of output to a different kind is " | ||
| "deprecated. " | ||
| "In a future version, this will result in an error. (Deprecated NumPy 2.5)") < |
There was a problem hiding this comment.
Can you format this a bit prettier? (e.g. start at smaller indent, don't break the line when there is no \n anyway.
| dtype = PyArray_DESCR(self); | ||
| out_dtype = PyArray_DESCR(out); | ||
| if (dtype != out_dtype) { | ||
| /*Deprecated NumPy 2.5, 2026-01*/ |
There was a problem hiding this comment.
| /*Deprecated NumPy 2.5, 2026-01*/ | |
| /* Deprecated NumPy 2.5, 2026-01 */ |
There was a problem hiding this comment.
"Updated as suggested."
| out_dtype = PyArray_DESCR(out); | ||
| if (dtype != out_dtype) { | ||
| /*Deprecated NumPy 2.5, 2026-01*/ | ||
| if (PyArray_CanCastTypeTo(dtype, out_dtype, NPY_SAME_KIND_CASTING) == 0) { |
There was a problem hiding this comment.
Most code uses !PyArray_... for this type of pattern, so I would stick to it here too. (The < 0 is very common for errors.)
There was a problem hiding this comment.
Fixed in 6f40af4. I've updated this to follow the !PyArray_... pattern.
| raise AssertionError("compress with an out which cannot be " | ||
| "safely casted should not return " | ||
| "successfully") | ||
| assert_equal(b, np.array([[1.0], [3.0]])) |
There was a problem hiding this comment.
OK, I guess this is fine to generalize. We may want to tighten the casting from same-kind at some point, but I don't think we have to worry about it now.
|
|
||
| self.assert_deprecated( | ||
| np.take, args=(a, indices), kwargs={"out": diffrent_dtype_out} | ||
| ) |
There was a problem hiding this comment.
different_dtype_out or just shorten (small typo). This is good as is, but while I wrote this args/kwargs, I actually like the pattern we use a lot more to just use a lambda, i.e. passing lambda: np.take(a, indices, out=out) (it even ends up shorter!)
There was a problem hiding this comment.
Fixed in 6f40af4. I've renamed the variable to different_dtype_out. I also switched to the lambda pattern as you suggested—it definitely looks much cleaner and more concise.
|
Thanks for the thorough review. I’ve updated the PR and addressed each comment. |
6f40af4 to
e0a9dfd
Compare
|
I have resolved the merge conflicts and rebased the branch onto the latest main. |
|
Hi @seberg , sorry for the ping, but I'm just checking in to see if there's anything else I should update for this PR. I’d appreciate any feedback when you have a moment" |
|
Sorry, I forgot about this. We have conflicts now, and it would make sense to add a very brief (single bullet point is OK) release note for the new deprecation. |
|
Thank you! I appreciate you taking care of the conflicts and the release note. I look forward to it. |
Previously, an error was raised when the input and output dtypes were different during casting. This change allows 'same-kind' casting to proceed while issuing a DeprecationWarning for other casting types to maintain backward compatibility while signaling future changes. Closes numpy#25588
Description
This PR addresses the issue in
np.takewhere an error was raised when input and output dtypes were different during casting.Key changes:
np.take.DeprecationWarningfor other casting types to maintain backward compatibility while signaling future changes.This is my first contribution to NumPy, so I'm opening this as a draft to ensure the implementation and formatting align with the project's standards. I'd appreciate any feedback on the code or the CI results.
Fixes
Closes #25588