Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 754252c

Browse filesBrowse files
committed
TYP: preserve shape in ndarray.astype()
This patch changes the return type in astype() from NDArray to ndarray so that shape information is preserved and adds tests for it.
1 parent bbf4836 commit 754252c
Copy full SHA for 754252c

File tree

Expand file treeCollapse file tree

2 files changed

+7
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+7
-2
lines changed

‎numpy/__init__.pyi

Copy file name to clipboardExpand all lines: numpy/__init__.pyi
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,7 +2496,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DType_co]):
24962496
casting: _CastingKind = ...,
24972497
subok: builtins.bool = ...,
24982498
copy: builtins.bool | _CopyMode = ...,
2499-
) -> NDArray[_SCT]: ...
2499+
) -> ndarray[_ShapeT_co, dtype[_SCT]]: ...
25002500
@overload
25012501
def astype(
25022502
self,
@@ -2505,7 +2505,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DType_co]):
25052505
casting: _CastingKind = ...,
25062506
subok: builtins.bool = ...,
25072507
copy: builtins.bool | _CopyMode = ...,
2508-
) -> NDArray[Any]: ...
2508+
) -> ndarray[_ShapeT_co, np.dtype[Any]]: ...
25092509

25102510
@overload
25112511
def view(self) -> Self: ...

‎numpy/typing/tests/data/reveal/ndarray_conversion.pyi

Copy file name to clipboardExpand all lines: numpy/typing/tests/data/reveal/ndarray_conversion.pyi
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ i4_2d: np.ndarray[tuple[int, int], np.dtype[np.int32]]
1111
f8_3d: np.ndarray[tuple[int, int, int], np.dtype[np.float64]]
1212
cG_4d: np.ndarray[tuple[int, int, int, int], np.dtype[np.clongdouble]]
1313
i0_nd: npt.NDArray[np.int_]
14+
uncertain_dtype: np.int32 | np.float64 | np.str_
1415

1516
# item
1617
assert_type(i0_nd.item(), int)
@@ -50,6 +51,10 @@ assert_type(i0_nd.astype(np.float64, "K", "unsafe", True, True), npt.NDArray[np.
5051

5152
assert_type(np.astype(i0_nd, np.float64), npt.NDArray[np.float64])
5253

54+
assert_type(i4_2d.astype(np.uint16), np.ndarray[tuple[int, int], np.dtype[np.uint16]])
55+
assert_type(f8_3d.astype(np.int16), np.ndarray[tuple[int, int, int], np.dtype[np.int16]])
56+
assert_type(i4_2d.astype(uncertain_dtype), np.ndarray[tuple[int, int], np.dtype[np.generic[Any]]])
57+
5358
# byteswap
5459
assert_type(i0_nd.byteswap(), npt.NDArray[np.int_])
5560
assert_type(i0_nd.byteswap(True), npt.NDArray[np.int_])

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.