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

TYP: np.argmin and np.argmax overload changes #28906

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 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 9 additions & 8 deletions 17 numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ _CharacterItemT_co = TypeVar("_CharacterItemT_co", bound=_CharLike_co, default=_
_TD64ItemT_co = TypeVar("_TD64ItemT_co", bound=dt.timedelta | int | None, default=dt.timedelta | int | None, covariant=True)
_DT64ItemT_co = TypeVar("_DT64ItemT_co", bound=dt.date | int | None, default=dt.date | int | None, covariant=True)
_TD64UnitT = TypeVar("_TD64UnitT", bound=_TD64Unit, default=_TD64Unit)
_BoolOrIntArrayT = TypeVar("_BoolOrIntArrayT", bound=NDArray[integer | np.bool])

### Type Aliases (for internal use only)

Expand Down Expand Up @@ -1704,18 +1705,18 @@ class _ArrayOrScalarCommon:
@overload # axis=index, out=None (default)
def argmax(self, /, axis: SupportsIndex, out: None = None, *, keepdims: builtins.bool = False) -> Any: ...
@overload # axis=index, out=ndarray
def argmax(self, /, axis: SupportsIndex | None, out: _ArrayT, *, keepdims: builtins.bool = False) -> _ArrayT: ...
def argmax(self, /, axis: SupportsIndex | None, out: _BoolOrIntArrayT, *, keepdims: builtins.bool = False) -> _BoolOrIntArrayT: ...
@overload
def argmax(self, /, axis: SupportsIndex | None = None, *, out: _ArrayT, keepdims: builtins.bool = False) -> _ArrayT: ...
def argmax(self, /, axis: SupportsIndex | None = None, *, out: _BoolOrIntArrayT, keepdims: builtins.bool = False) -> _BoolOrIntArrayT: ...

@overload # axis=None (default), out=None (default), keepdims=False (default)
def argmin(self, /, axis: None = None, out: None = None, *, keepdims: L[False] = False) -> intp: ...
@overload # axis=index, out=None (default)
def argmin(self, /, axis: SupportsIndex, out: None = None, *, keepdims: builtins.bool = False) -> Any: ...
@overload # axis=index, out=ndarray
def argmin(self, /, axis: SupportsIndex | None, out: _ArrayT, *, keepdims: builtins.bool = False) -> _ArrayT: ...
def argmin(self, /, axis: SupportsIndex | None, out: _BoolOrIntArrayT, *, keepdims: builtins.bool = False) -> _BoolOrIntArrayT: ...
@overload
def argmin(self, /, axis: SupportsIndex | None = None, *, out: _ArrayT, keepdims: builtins.bool = False) -> _ArrayT: ...
def argmin(self, /, axis: SupportsIndex | None = None, *, out: _BoolOrIntArrayT, keepdims: builtins.bool = False) -> _BoolOrIntArrayT: ...

@overload # out=None (default)
def round(self, /, decimals: SupportsIndex = 0, out: None = None) -> Self: ...
Expand Down Expand Up @@ -5364,19 +5365,19 @@ class matrix(ndarray[_2DShapeT_co, _DTypeT_co]):
@overload
def argmax(self, axis: _ShapeLike, out: None = None) -> matrix[_2D, dtype[intp]]: ...
@overload
def argmax(self, axis: _ShapeLike | None, out: _ArrayT) -> _ArrayT: ...
def argmax(self, axis: _ShapeLike | None, out: _BoolOrIntArrayT) -> _BoolOrIntArrayT: ...
@overload
def argmax(self, axis: _ShapeLike | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
def argmax(self, axis: _ShapeLike | None = None, *, out: _BoolOrIntArrayT) -> _BoolOrIntArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]

# keep in sync with `argmax`
@overload # type: ignore[override]
def argmin(self: NDArray[_ScalarT], axis: None = None, out: None = None) -> intp: ...
@overload
def argmin(self, axis: _ShapeLike, out: None = None) -> matrix[_2D, dtype[intp]]: ...
@overload
def argmin(self, axis: _ShapeLike | None, out: _ArrayT) -> _ArrayT: ...
def argmin(self, axis: _ShapeLike | None, out: _BoolOrIntArrayT) -> _BoolOrIntArrayT: ...
@overload
def argmin(self, axis: _ShapeLike | None = None, *, out: _ArrayT) -> _ArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]
def argmin(self, axis: _ShapeLike | None = None, *, out: _BoolOrIntArrayT) -> _BoolOrIntArrayT: ... # pyright: ignore[reportIncompatibleMethodOverride]

#the second overload handles the (rare) case that the matrix is not 2-d
@overload
Expand Down
17 changes: 9 additions & 8 deletions 17 numpy/_core/fromnumeric.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ _NumberOrObjectT = TypeVar("_NumberOrObjectT", bound=np.number | np.object_)
_ArrayT = TypeVar("_ArrayT", bound=np.ndarray[Any, Any])
_ShapeT = TypeVar("_ShapeT", bound=tuple[int, ...])
_ShapeT_co = TypeVar("_ShapeT_co", bound=tuple[int, ...], covariant=True)
_BoolOrIntArrayT = TypeVar("_BoolOrIntArrayT", bound=NDArray[np.integer | np.bool])

@type_check_only
class _SupportsShape(Protocol[_ShapeT_co]):
Expand Down Expand Up @@ -418,18 +419,18 @@ def argmax(
def argmax(
a: ArrayLike,
axis: SupportsIndex | None,
out: _ArrayT,
out: _BoolOrIntArrayT,
*,
keepdims: bool = ...,
) -> _ArrayT: ...
) -> _BoolOrIntArrayT: ...
@overload
def argmax(
a: ArrayLike,
axis: SupportsIndex | None = ...,
*,
out: _ArrayT,
out: _BoolOrIntArrayT,
keepdims: bool = ...,
) -> _ArrayT: ...
) -> _BoolOrIntArrayT: ...

@overload
def argmin(
Expand All @@ -451,18 +452,18 @@ def argmin(
def argmin(
a: ArrayLike,
axis: SupportsIndex | None,
out: _ArrayT,
out: _BoolOrIntArrayT,
*,
keepdims: bool = ...,
) -> _ArrayT: ...
) -> _BoolOrIntArrayT: ...
@overload
def argmin(
a: ArrayLike,
axis: SupportsIndex | None = ...,
*,
out: _ArrayT,
out: _BoolOrIntArrayT,
keepdims: bool = ...,
) -> _ArrayT: ...
) -> _BoolOrIntArrayT: ...

@overload
def searchsorted(
Expand Down
3 changes: 3 additions & 0 deletions 3 numpy/typing/tests/data/fail/fromnumeric.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ A = np.array(True, ndmin=2, dtype=bool)
A.setflags(write=False)
AR_U: npt.NDArray[np.str_]
AR_M: npt.NDArray[np.datetime64]
AR_f4: npt.NDArray[np.float32]

a = np.bool(True)

Expand Down Expand Up @@ -50,9 +51,11 @@ np.argsort(A, order=range(5)) # type: ignore[arg-type]

np.argmax(A, axis="bob") # type: ignore[call-overload]
np.argmax(A, kind="bob") # type: ignore[call-overload]
np.argmax(A, out=AR_f4) # type: ignore[type-var]

np.argmin(A, axis="bob") # type: ignore[call-overload]
np.argmin(A, kind="bob") # type: ignore[call-overload]
np.argmin(A, out=AR_f4) # type: ignore[type-var]

np.searchsorted(A[0], 0, side="bob") # type: ignore[call-overload]
np.searchsorted(A[0], 0, sorter=1.0) # type: ignore[call-overload]
Expand Down
7 changes: 4 additions & 3 deletions 7 numpy/typing/tests/data/pass/ndarray_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import numpy.typing as npt

class SubClass(npt.NDArray[np.float64]): ...

class IntSubClass(npt.NDArray[np.intp]): ...

i4 = np.int32(1)
A: np.ndarray[Any, np.dtype[np.int32]] = np.array([[1]], dtype=np.int32)
B0 = np.empty((), dtype=np.int32).view(SubClass)
B1 = np.empty((1,), dtype=np.int32).view(SubClass)
B2 = np.empty((1, 1), dtype=np.int32).view(SubClass)
B_int0: IntSubClass = np.empty((), dtype=np.intp).view(IntSubClass)
C: np.ndarray[Any, np.dtype[np.int32]] = np.array([0, 1, 2], dtype=np.int32)
D = np.ones(3).view(SubClass)

Expand All @@ -42,12 +43,12 @@ class SubClass(npt.NDArray[np.float64]): ...
i4.argmax()
A.argmax()
A.argmax(axis=0)
A.argmax(out=B0)
A.argmax(out=B_int0)

i4.argmin()
A.argmin()
A.argmin(axis=0)
A.argmin(out=B0)
A.argmin(out=B_int0)

i4.argsort()
A.argsort()
Expand Down
8 changes: 6 additions & 2 deletions 8 numpy/typing/tests/data/reveal/fromnumeric.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ f4: np.float32
i8: np.int64
f: float

# integer‑dtype subclass for argmin/argmax
class NDArrayIntSubclass(npt.NDArray[np.intp]): ...
AR_sub_i: NDArrayIntSubclass

assert_type(np.take(b, 0), np.bool)
assert_type(np.take(f4, 0), np.float32)
assert_type(np.take(f, 0), Any)
Expand Down Expand Up @@ -89,13 +93,13 @@ assert_type(np.argmax(AR_b), np.intp)
assert_type(np.argmax(AR_f4), np.intp)
assert_type(np.argmax(AR_b, axis=0), Any)
assert_type(np.argmax(AR_f4, axis=0), Any)
assert_type(np.argmax(AR_f4, out=AR_subclass), NDArraySubclass)
assert_type(np.argmax(AR_f4, out=AR_sub_i), NDArrayIntSubclass)

assert_type(np.argmin(AR_b), np.intp)
assert_type(np.argmin(AR_f4), np.intp)
assert_type(np.argmin(AR_b, axis=0), Any)
assert_type(np.argmin(AR_f4, axis=0), Any)
assert_type(np.argmin(AR_f4, out=AR_subclass), NDArraySubclass)
assert_type(np.argmin(AR_f4, out=AR_sub_i), NDArrayIntSubclass)

assert_type(np.searchsorted(AR_b[0], 0), np.intp)
assert_type(np.searchsorted(AR_f4[0], 0), np.intp)
Expand Down
5 changes: 3 additions & 2 deletions 5 numpy/typing/tests/data/reveal/matrix.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ _Shape2D: TypeAlias = tuple[int, int]

mat: np.matrix[_Shape2D, np.dtype[np.int64]]
ar_f8: npt.NDArray[np.float64]
ar_ip: npt.NDArray[np.intp]

assert_type(mat * 5, np.matrix[_Shape2D, Any])
assert_type(5 * mat, np.matrix[_Shape2D, Any])
Expand Down Expand Up @@ -50,8 +51,8 @@ assert_type(mat.any(out=ar_f8), npt.NDArray[np.float64])
assert_type(mat.all(out=ar_f8), npt.NDArray[np.float64])
assert_type(mat.max(out=ar_f8), npt.NDArray[np.float64])
assert_type(mat.min(out=ar_f8), npt.NDArray[np.float64])
assert_type(mat.argmax(out=ar_f8), npt.NDArray[np.float64])
assert_type(mat.argmin(out=ar_f8), npt.NDArray[np.float64])
assert_type(mat.argmax(out=ar_ip), npt.NDArray[np.intp])
assert_type(mat.argmin(out=ar_ip), npt.NDArray[np.intp])
assert_type(mat.ptp(out=ar_f8), npt.NDArray[np.float64])

assert_type(mat.T, np.matrix[_Shape2D, np.dtype[np.int64]])
Expand Down
4 changes: 2 additions & 2 deletions 4 numpy/typing/tests/data/reveal/ndarray_misc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ assert_type(AR_f8.any(out=B), SubClass)
assert_type(f8.argmax(), np.intp)
assert_type(AR_f8.argmax(), np.intp)
assert_type(AR_f8.argmax(axis=0), Any)
assert_type(AR_f8.argmax(out=B), SubClass)
assert_type(AR_f8.argmax(out=AR_i8), npt.NDArray[np.intp])

assert_type(f8.argmin(), np.intp)
assert_type(AR_f8.argmin(), np.intp)
assert_type(AR_f8.argmin(axis=0), Any)
assert_type(AR_f8.argmin(out=B), SubClass)
assert_type(AR_f8.argmin(out=AR_i8), npt.NDArray[np.intp])

assert_type(f8.argsort(), npt.NDArray[Any])
assert_type(AR_f8.argsort(), npt.NDArray[Any])
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.