Skip to content

Commit fed445b

Browse files
committed
Apply review comments
1 parent 403a9bf commit fed445b

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

doc/source/user/absolute_beginners.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ this array to an array with three rows and two columns::
425425

426426
With ``np.reshape``, you can specify a few optional parameters::
427427

428-
>>> np.reshape(a, newshape=(1, 6), order='C')
428+
>>> np.reshape(a, shape=(1, 6), order='C')
429429
array([[0, 1, 2, 3, 4, 5]])
430430

431431
``a`` is the array to be reshaped.

numpy/_core/fromnumeric.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,28 +206,28 @@ def take(a, indices, axis=None, out=None, mode='raise'):
206206
return _wrapfunc(a, 'take', indices, axis=axis, out=out, mode=mode)
207207

208208

209-
def _reshape_dispatcher(a, /, newshape=None, shape=None, *,
210-
order=None, copy=None):
209+
def _reshape_dispatcher(a, /, shape=None, *, newshape=None, order=None,
210+
copy=None):
211211
return (a,)
212212

213213

214214
@array_function_dispatch(_reshape_dispatcher)
215-
def reshape(a, /, newshape=None, shape=None, *, order='C', copy=None):
215+
def reshape(a, /, shape=None, *, newshape=None, order='C', copy=None):
216216
"""
217217
Gives a new shape to an array without changing its data.
218218
219219
Parameters
220220
----------
221221
a : array_like
222222
Array to be reshaped.
223-
newshape : int or tuple of ints
224-
Replaced by ``shape`` argument. Retained for backward
225-
compatibility.
226223
shape : int or tuple of ints
227224
The new shape should be compatible with the original shape. If
228225
an integer, then the result will be a 1-D array of that length.
229226
One shape dimension can be -1. In this case, the value is
230227
inferred from the length of the array and remaining dimensions.
228+
newshape : int or tuple of ints
229+
Replaced by ``shape`` argument. Retained for backward
230+
compatibility.
231231
order : {'C', 'F', 'A'}, optional
232232
Read the elements of ``a`` using this index order, and place the
233233
elements into the reshaped array using this index order. 'C'
@@ -310,9 +310,17 @@ def reshape(a, /, newshape=None, shape=None, *, order='C', copy=None):
310310
raise ValueError(
311311
"You cannot specify 'newshape' and 'shape' arguments "
312312
"at the same time.")
313-
if shape is not None:
314-
newshape = shape
315-
return _wrapfunc(a, 'reshape', newshape, order=order, copy=copy)
313+
if newshape is not None:
314+
# Deprecated in NumPy 2.1, 2024-04-18
315+
warnings.warn(
316+
"`newshape` keyword argument is deprecated, "
317+
"use `shape=...` or pass shape positionally instead. "
318+
"(deprecated in NumPy 2.1)",
319+
DeprecationWarning,
320+
stacklevel=2,
321+
)
322+
shape = newshape
323+
return _wrapfunc(a, 'reshape', shape, order=order, copy=copy)
316324

317325

318326
def _choose_dispatcher(a, choices, out=None, mode=None):

numpy/_core/tests/test_numeric.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def test_reshape_shape_arg(self):
175175
match="You cannot specify 'newshape' and 'shape' "
176176
"arguments at the same time."
177177
):
178-
np.reshape(arr, newshape=shape, shape=shape)
178+
np.reshape(arr, shape=shape, newshape=shape)
179179
with pytest.raises(
180180
TypeError,
181181
match=r"reshape\(\) missing 1 required positional "
@@ -185,9 +185,11 @@ def test_reshape_shape_arg(self):
185185

186186
assert_equal(np.reshape(arr, shape), expected)
187187
assert_equal(np.reshape(arr, shape, order="C"), expected)
188-
assert_equal(np.reshape(arr, newshape=shape), expected)
189188
assert_equal(np.reshape(arr, shape=shape), expected)
190189
assert_equal(np.reshape(arr, shape=shape, order="C"), expected)
190+
with pytest.warns(DeprecationWarning):
191+
actual = np.reshape(arr, newshape=shape)
192+
assert_equal(actual, expected)
191193

192194
def test_reshape_copy_arg(self):
193195
arr = np.arange(24).reshape(2, 3, 4)

0 commit comments

Comments
 (0)