8000 review comments · numpy/numpy@c27f61a · GitHub
[go: up one dir, main page]

Skip to content

Commit c27f61a

Browse files
committed
review comments
1 parent 76303cf commit c27f61a

File tree

6 files changed

+59
-13
lines changed

6 files changed

+59
-13
lines changed

numpy/__init__.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ from typing import (
216216
# library include `typing_extensions` stubs:
217217
# https://github.com/python/typeshed/blob/main/stdlib/typing_extensions.pyi
218218
from _typeshed import StrOrBytesPath, SupportsFlush, SupportsLenAndGetItem, SupportsWrite
219-
from typing_extensions import CapsuleType, TypeVar
219+
from typing_extensions import deprecated, CapsuleType, TypeVar
220220

221221
from numpy import (
222222
char,
@@ -2152,6 +2152,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
21522152
def shape(self, value: _ShapeLike) -> None: ...
21532153
@property
21542154
def strides(self) -> _Shape: ...
2155+
@deprecated("Setting the strides on a NumPy array has been deprecated in Numpy 2.3")
21552156
@strides.setter
21562157
def strides(self, value: _ShapeLike) -> None: ...
21572158
def byteswap(self, inplace: builtins.bool = ...) -> Self: ...

numpy/_core/records.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,10 @@ def __array_finalize__(self, obj):
410410
if self.dtype.type is not record and self.dtype.names is not None:
411411
# if self.dtype is not np.record, invoke __setattr__ which will
412412
# convert it to a record if it is a void dtype.
413-
self.dtype = self.dtype
413+
with warnings.catch_warnings():
414+
# gh-28901
415+
warnings.filterwarnings("ignore", category=DeprecationWarning)
416+
self.dtype = self.dtype
414417

415418
def __getattribute__(self, attr):
416419
# See if ndarray has this attr, and return it if so. (note that this
@@ -459,7 +462,13 @@ def __setattr__(self, attr, val):
459462

460463
newattr = attr not in self.__dict__
461464
try:
462-
ret = object.__setattr__(self, attr, val)
465+
if attr == 'dtype':
466+
with warnings.catch_warnings():
467+
# gh-28901
468+
warnings.filterwarnings("ignore", category=DeprecationWarning)
469+
ret = object.__setattr__(self, attr, val)
470+
else:
471+
ret = object.__setattr__(self, attr, val)
463472
except Exception:
464473
fielddict = ndarray.__getattribute__(self, 'dtype').fields or {}
465474
if attr not in fielddict:

numpy/_core/src/multiarray/getset.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,12 @@ array_strides_set(PyArrayObject *self, PyObject *obj, void *NPY_UNUSED(ignored))
125125
Py_buffer view;
126126

127127
/* DEPRECATED 2025-05-04, NumPy 2.3 */
128-
PyErr_WarnEx(PyExc_DeprecationWarning,
129-
"Setting the strides on a Numpy array has been deprecated in Numpy 2.3.\n",
128+
int ret = PyErr_WarnEx(PyExc_DeprecationWarning,
129+
"Setting the strides on a NumPy array has been deprecated in NumPy 2.3.\n",
130130
1);
131+
if (ret) {
132+
return -1;
133+
}
131134

132135
if (obj == NULL) {
133136
PyErr_SetString(PyExc_AttributeError,
@@ -375,13 +378,22 @@ array_nbytes_get(PyArrayObject *self, void *NPY_UNUSED(ignored))
375378
static int
376379
array_descr_set(PyArrayObject *self, PyObject *arg, void *NPY_UNUSED(ignored))
377380
{
378-
PyArray_Descr *newtype = NULL;
381+
// to be replaced with PyUnstable_Object_IsUniquelyReferenced https://github.com/python/cpython/pull/133144
382+
int unique_reference = (Py_REFCNT(self) == 1);
383+
384+
if (!unique_reference) {
385+
// this will not emit deprecation warnings for all cases, but for most it will
386+
/* DEPRECATED 2025-05-04, NumPy 2.3 */
387+
int ret = PyErr_WarnEx(PyExc_DeprecationWarning,
388+
"Setting the dtype on a NumPy array has been deprecated in NumPy 2.3.\n"
389+
"Instead of changing the dtype on an array x, create a new array with x.view(new_dtype)",
390+
1);
391+
if (ret) {
392+
return -1;
393+
}
394+
}
379395

380-
/* DEPRECATED 2025-05-04, NumPy 2.3 */
381-
PyErr_WarnEx(PyExc_DeprecationWarning,
382-
"Setting the dtype on a Numpy array has been deprecated in Numpy 2.3.\n"
383-
"Instead of changing the dtype on an array x, create a new array with numpy.frombuffer(x, dtype=new_dtype)",
384-
1);
396+
PyArray_Descr *newtype = NULL;
385397

386398
if (arg == NULL) {
387399
PyErr_SetString(PyExc_AttributeError,

numpy/_core/tests/test_deprecations.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,24 @@ def __array_wrap__(self, arr):
414414
self.assert_deprecated(lambda: np.negative(test2))
415415
assert test2.called
416416

417+
class TestDeprecatedArrayAttributeSetting(_DeprecationTestCase):
418+
message = "Setting the .*on a NumPy array has been deprecated.*"
417419

420+
def test_deprecated_dtype_set(self):
421+
x = np.eye(2)
422+
423+
def set_dtype(x):
424+
x.dtype = int
425+
self.assert_deprecated(lambda: set_dtype(x))
426+
427+
def test_deprecated_strides_set(self):
428+
x = np.eye(2)
429+
430+
def set_strides(x):
431+
s = x.strides
432+
x.strides = s
433+
434+
self.assert_deprecated(lambda: set_strides(x))
418435
class TestDeprecatedDTypeParenthesizedRepeatCount(_DeprecationTestCase):
419436
message = "Passing in a parenthesized single number"
420437

numpy/lib/_stride_tricks_impl.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
An explanation of strides can be found in the :ref:`arrays.ndarray`.
55
66
"""
7+
import warnings
78
import numpy as np
89
from numpy._core.numeric import normalize_axis_tuple
910
from numpy._core.overrides import array_function_dispatch, set_module
@@ -101,7 +102,10 @@ def as_strided(x, shape=None, strides=None, subok=False, writeable=True):
101102
array = np.asarray(DummyArray(interface, base=x))
102103
# The route via `__interface__` does not preserve structured
103104
# dtypes. Since dtype should remain unchanged, we set it explicitly.
104-
array.dtype = x.dtype
105+
with warnings.catch_warnings():
106+
# gh-28901
107+
warnings.filterwarnings("ignore", category=DeprecationWarning)
108+
array.dtype = x.dtype
105109

106110
view = _maybe_view_as_subclass(x, array)
107111

numpy/ma/core.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3492,7 +3492,10 @@ def dtype(self):
34923492

34933493
@dtype.setter
34943494
def dtype(self, dtype):
3495-
super(MaskedArray, type(self)).dtype.__set__(self, dtype)
3495+
with warnings.catch_warnings():
3496+
# gh-28901
3497+
warnings.filterwarnings("ignore", category=DeprecationWarning)
3498+
super(MaskedArray, type(self)).dtype.__set__(self, dtype)
34963499
if self._mask is not nomask:
34973500
self._mask = self._mask.view(make_mask_descr(dtype), ndarray)
34983501
# Try to reset the shape of the mask (if we don't have a void).

0 commit comments

Comments
 (0)
0