Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
jorenham merged 1 commit intonumpy:mainfromlvllvl:issue-28641
May 16, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsnumpy/__init__.pyi
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionsnumpy/_core/fromnumeric.pyi
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsnumpy/typing/tests/data/fail/fromnumeric.pyi
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsnumpy/typing/tests/data/pass/ndarray_misc.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsnumpy/typing/tests/data/reveal/fromnumeric.pyi
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsnumpy/typing/tests/data/reveal/matrix.pyi
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsnumpy/typing/tests/data/reveal/ndarray_misc.pyi
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp