|
| 1 | +Cast-safety fixes in ``copyto`` and ``full`` |
| 2 | +-------------------------------------------- |
| 3 | +``copyto`` now uses NEP 50 correctly and applies this to its cast safety. |
| 4 | +Python integer to NumPy integer casts and Python float to NumPy float casts |
| 5 | +are now considered "safe" even if assignment may fail or precision may be lost. |
| 6 | +This means the following examples change slightly: |
| 7 | + |
| 8 | +* ``np.copyto(int8_arr, 1000)`` previously performed an unsafe/same-kind cast |
| 9 | + of the Python integer. It will now always raise, to achieve an unsafe cast |
| 10 | + you must pass an array or NumPy scalar. |
| 11 | +* ``np.copyto(uint8_arr, 1000, casting="safe")`` will raise an OverflowError |
| 12 | + rather than a TypeError due to same-kind casting. |
| 13 | +* ``np.copyto(float32_arr, 1e300, casting="safe")`` will overflow to ``inf`` |
| 14 | + (float32 cannot hold ``1e300``) rather raising a TypeError. |
| 15 | + |
| 16 | +Further, only the dtype is used when assigning NumPy scalars (or 0-d arrays), |
| 17 | +meaning that the following behaves differently: |
| 18 | + |
| 19 | +* ``np.copyto(float32_arr, np.float64(3.0), casting="safe")`` raises. |
| 20 | +* ``np.coptyo(int8_arr, np.int64(100), casting="safe")`` raises. |
| 21 | + Previously, NumPy checked whether the 100 fits the ``int8_arr``. |
| 22 | + |
| 23 | +This aligns ``copyto``, ``full``, and ``full_like`` with the correct NumPy 2 |
| 24 | +behavior. |
0 commit comments