8000 TYP: Support shape-typing in ``reshape`` and ``resize`` by jorenham · Pull Request #27767 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

TYP: Support shape-typing in reshape and resize #27767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
TYP: Shape-typed np.resize annotations
  • Loading branch information
jorenham committed Nov 14, 2024
commit d82e4401803006cc168dc5c86fa637e0b60d256d
24 changes: 16 additions & 8 deletions numpy/_core/fromnumeric.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ __all__ = [
_SCT = TypeVar("_SCT", bound=generic)
_SCT_uifcO = TypeVar("_SCT_uifcO", bound=number[Any] | object_)
_ArrayType = TypeVar("_ArrayType", bound=np.ndarray[Any, Any])
_SizeType = TypeVar("_SizeType", bound=int)
_ShapeType = TypeVar("_ShapeType", bound=tuple[int, ...])
_ShapeType_co = TypeVar("_ShapeType_co", bound=tuple[int, ...], covariant=True)

Expand Down Expand Up @@ -427,16 +428,23 @@ def searchsorted(
sorter: None | _ArrayLikeInt_co = ..., # 1D int array
) -> NDArray[intp]: ...

# unlike `reshape`, `resize` only accepts positive integers, so literal ints can be used
@overload
def resize(
a: _ArrayLike[_SCT],
new_shape: _ShapeLike,
) -> NDArray[_SCT]: ...
def resize(a: _ArrayLike[_SCT], new_shape: _SizeType) -> np.ndarray[tuple[_SizeType], np.dtype[_SCT]]: ...
@overload
def resize(
a: ArrayLike,
new_shape: _ShapeLike,
) -> NDArray[Any]: ...
def resize(a: _ArrayLike[_SCT], new_shape: SupportsIndex) -> np.ndarray[tuple[int], np.dtype[_SCT]]: ...
@overload
def resize(a: _ArrayLike[_SCT], new_shape: _ShapeType) -> np.ndarray[_ShapeType, np.dtype[_SCT]]: ...
@overload
def resize(a: _ArrayLike[_SCT], new_shape: Sequence[SupportsIndex]) -> NDArray[_SCT]: ...
@overload
def resize(a: ArrayLike, new_shape: _SizeType) -> np.ndarray[tuple[_SizeType], np.dtype[Any]]: ...
@overload
def resize(a: ArrayLike, new_shape: SupportsIndex) -> np.ndarray[tuple[int], np.dtype[Any]]: ...
@overload
def resize(a: ArrayLike, new_shape: _ShapeType) -> np.ndarray[_ShapeType, np.dtype[Any]]: ...
@overload
def resize(a: ArrayLike, new_shape: Sequence[SupportsIndex]) -> NDArray[Any]: ...

@overload
def squeeze(
Expand Down
12 changes: 6 additions & 6 deletions numpy/typing/tests/data/reveal/fromnumeric.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Tests for :mod:`_core.fromnumeric`."""

from typing import Any, NoReturn
from typing import Any, Literal as L, NoReturn

import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -102,11 +102,11 @@ assert_type(np.searchsorted(AR_f4[0], 0), np.intp)
assert_type(np.searchsorted(AR_b[0], [0]), npt.NDArray[np.intp])
assert_type(np.searchsorted(AR_f4[0], [0]), npt.NDArray[np.intp])

assert_type(np.resize(b, (5, 5)), npt.NDArray[np.bool])
assert_type(np.resize(f4, (5, 5)), npt.NDArray[np.float32])
assert_type(np.resize(f, (5, 5)), npt.NDArray[Any])
assert_type(np.resize(AR_b, (5, 5)), npt.NDArray[np.bool])
assert_type(np.resize(AR_f4, (5, 5)), npt.NDArray[np.float32])
assert_type(np.resize(b, (5, 5)), np.ndarray[tuple[L[5], L[5]], np.dtype[np.bool]])
assert_type(np.resize(f4, (5, 5)), np.ndarray[tuple[L[5], L[5]], np.dtype[np.float32]])
assert_type(np.resize(f, (5, 5)), np.ndarray[tuple[L[5], L[5]], np.dtype[Any]])
assert_type(np.resize(AR_b, (5, 5)), np.ndarray[tuple[L[5], L[5]], np.dtype[np.bool]])
assert_type(np.resize(AR_f4, (5, 5)), np.ndarray[tuple[L[5], L[5]], np.dtype[np.float32]])

assert_type(np.squeeze(b), np.bool)
assert_type(np.squeeze(f4), np.float32)
Expand Down
Loading
0