Skip to content

Commit db19c9c

Browse files
committed
TYP: Shape-typed np.reshape, and deprecate the newshape kwarg
1 parent cb6f5fe commit db19c9c

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

numpy/_core/fromnumeric.pyi

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ from typing import (
1010
overload,
1111
type_check_only,
1212
)
13-
from typing_extensions import Never
13+
from typing_extensions import Never, deprecated
1414

1515
import numpy as np
1616
from numpy import (
@@ -25,6 +25,7 @@ from numpy import (
2525
timedelta64,
2626
object_,
2727
generic,
28+
_AnyShapeType,
2829
_OrderKACF,
2930
_OrderACF,
3031
_ModeKind,
@@ -161,24 +162,73 @@ def take(
161162
) -> _ArrayType: ...
162163

163164
@overload
165+
def reshape( # shape: index
166+
a: _ArrayLike[_SCT],
167+
/,
168+
shape: SupportsIndex,
169+
order: _OrderACF = "C",
170+
*,
171+
copy: bool | None = None,
172+
) -> np.ndarray[tuple[int], np.dtype[_SCT]]: ...
173+
@overload
174+
def reshape( # shape: (int, ...) @ _AnyShapeType
175+
a: _ArrayLike[_SCT],
176+
/,
177+
shape: _AnyShapeType,
178+
order: _OrderACF = "C",
179+
*,
180+
copy: bool | None = None,
181+
) -> np.ndarray[_AnyShapeType, np.dtype[_SCT]]: ...
182+
@overload # shape: Sequence[index]
164183
def reshape(
165184
a: _ArrayLike[_SCT],
166185
/,
167-
shape: _ShapeLike = ...,
168-
order: _OrderACF = ...,
186+
shape: Sequence[SupportsIndex],
187+
order: _OrderACF = "C",
169188
*,
170-
newshape: _ShapeLike = ...,
171-
copy: None | bool = ...,
189+
copy: bool | None = None,
172190
) -> NDArray[_SCT]: ...
191+
@overload # shape: index
192+
def reshape(
193+
a: ArrayLike,
194+
/,
195+
shape: SupportsIndex,
196+
order: _OrderACF = "C",
197+
*,
198+
copy: bool | None = None,
199+
) -> np.ndarray[tuple[int], np.dtype[Any]]: ...
173200
@overload
201+
def reshape( # shape: (int, ...) @ _AnyShapeType
202+
a: ArrayLike,
203+
/,
204+
shape: _AnyShapeType,
205+
order: _OrderACF = "C",
206+
*,
207+
copy: bool | None = None,
208+
) -> np.ndarray[_AnyShapeType, np.dtype[Any]]: ...
209+
@overload # shape: Sequence[index]
210+
def reshape(
211+
a: ArrayLike,
212+
/,
213+
shape: Sequence[SupportsIndex],
214+
order: _OrderACF = "C",
215+
*,
216+
copy: bool | None = None,
217+
) -> NDArray[Any]: ...
218+
@overload
219+
@deprecated(
220+
"`newshape` keyword argument is deprecated, "
221+
"use `shape=...` or pass shape positionally instead. "
222+
"(deprecated in NumPy 2.1)",
223+
)
174224
def reshape(
175225
a: ArrayLike,
176226
/,
177-
shape: _ShapeLike = ...,
178-
order: _OrderACF = ...,
227+
shape: None = None,
228+
order: _OrderACF = "C",
179229
*,
180-
newshape: _ShapeLike = ...,
181-
copy: None | bool = ...,
230+
newshape: _ShapeLike,
231+
copy: bool | None = None,
182232
) -> NDArray[Any]: ...
183233

184234
@overload

numpy/typing/tests/data/reveal/fromnumeric.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ assert_type(np.take(AR_f4, [0]), npt.NDArray[np.float32])
3737
assert_type(np.take([1], [0]), npt.NDArray[Any])
3838
assert_type(np.take(AR_f4, [0], out=AR_subclass), NDArraySubclass)
3939

40-
assert_type(np.reshape(b, 1), npt.NDArray[np.bool])
41-
assert_type(np.reshape(f4, 1), npt.NDArray[np.float32])
42-
assert_type(np.reshape(f, 1), npt.NDArray[Any])
43-
assert_type(np.reshape(AR_b, 1), npt.NDArray[np.bool])
44-
assert_type(np.reshape(AR_f4, 1), npt.NDArray[np.float32])
40+
assert_type(np.reshape(b, 1), np.ndarray[tuple[int], np.dtype[np.bool]])
41+
assert_type(np.reshape(f4, 1), np.ndarray[tuple[int], np.dtype[np.float32]])
42+
assert_type(np.reshape(f, 1), np.ndarray[tuple[int], np.dtype[Any]])
43+
assert_type(np.reshape(AR_b, 1), np.ndarray[tuple[int], np.dtype[np.bool]])
44+
assert_type(np.reshape(AR_f4, 1), np.ndarray[tuple[int], np.dtype[np.float32]])
4545

4646
assert_type(np.choose(1, [True, True]), Any)
4747
assert_type(np.choose([1], [True, True]), npt.NDArray[Any])

0 commit comments

Comments
 (0)