-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
API,BUG: Fix copyto (and ufunc) handling of scalar cast safety #27091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1cb4044
API,BUG: Fix copyto (and ufunc) handling of scalar cast safety
seberg 7eb4050
MAINT: Remove value-based check (should be unused now)
seberg 97f6177
BUG: Fix windows default integer for resolver workaround
seberg 9ced09e
DOC: Add release note to change
seberg 8f03248
Apply suggestions from code review
seberg c5e3766
MAINt: Rename variables and at least only duplicate cleanup
seberg 42b58e3
Add an `n_cleanup` as requested in review
seberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Cast-safety fixes in ``copyto`` and ``full`` | ||
-------------------------------------------- | ||
``copyto`` now uses NEP 50 correctly and applies this to its cast safety. | ||
Python integer to NumPy integer casts and Python float to NumPy float casts | ||
are now considered "safe" even if assignment may fail or precision may be lost. | ||
This means the following examples change slightly: | ||
|
||
* ``np.copyto(int8_arr, 1000)`` previously performed an unsafe/same-kind cast | ||
of the Python integer. It will now always raise, to achieve an unsafe cast | ||
you must pass an array or NumPy scalar. | ||
* ``np.copyto(uint8_arr, 1000, casting="safe")`` will raise an OverflowError | ||
rather than a TypeError due to same-kind casting. | ||
* ``np.copyto(float32_arr, 1e300, casting="safe")`` will overflow to ``inf`` | ||
(float32 cannot hold ``1e300``) rather raising a TypeError. | ||
|
||
Further, only the dtype is used when assigning NumPy scalars (or 0-d arrays), | ||
meaning that the following behaves differently: | ||
|
||
* ``np.copyto(float32_arr, np.float64(3.0), casting="safe")`` raises. | ||
* ``np.coptyo(int8_arr, np.int64(100), casting="safe")`` raises. | ||
Previously, NumPy checked whether the 100 fits the ``int8_arr``. | ||
|
||
This aligns ``copyto``, ``full``, and ``full_like`` with the correct NumPy 2 | ||
behavior. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is intentional that I don't mention the ufunc changes. They are ridiculously niche IMO, since they require using
casting=
anddtype=
(or a weird casting likeequiv
).