8000 TYP: Type ``ma.MaskedArray.min`` (#28616) · numpy/numpy@5a8a1ed · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a8a1ed

Browse files
authored
TYP: Type ma.MaskedArray.min (#28616)
* TYP: Type MaskedArray.min * use _ArrayType for out * type: ignore[override] * go back to overload
1 parent 68a479a commit 5a8a1ed

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

numpy/ma/core.pyi

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# pyright: reportIncompatibleMethodOverride=false
22
# ruff: noqa: ANN001, ANN002, ANN003, ANN201, ANN202 ANN204
33

4-
from typing import Any, Literal, SupportsIndex, TypeVar, overload
4+
from typing import Any, Literal, SupportsIndex, TypeVar, overload, TypeAlias
55

66
from _typeshed import Incomplete
77
from typing_extensions import deprecated
@@ -211,8 +211,10 @@ _ShapeType = TypeVar("_ShapeType", bound=tuple[int, ...])
211211
_ShapeType_co = TypeVar("_ShapeType_co", bound=tuple[int, ...], covariant=True)
212212
_DType = TypeVar("_DType", bound=dtype[Any])
213213
_DType_co = TypeVar("_DType_co", bound=dtype[Any], covariant=True)
214-
_ArrayType = TypeVar("_ArrayType", bound=MaskedArray[Any, Any])
214+
_ArrayType = TypeVar("_ArrayType", bound=ndarray[Any, Any])
215215
_SCT = TypeVar("_SCT", bound=generic)
216+
# A subset of `MaskedArray` that can be parametrized w.r.t. `np.generic`
217+
_MaskedArray: TypeAlias = MaskedArray[Any, dtype[_SCT]]
216218

217219
MaskType = bool
218220
nomask: bool
@@ -466,7 +468,39 @@ class MaskedArray(ndarray[_ShapeType_co, _DType_co]):
466468
def argmin(self, axis=..., fill_value=..., out=..., *, keepdims=...): ...
467469
def argmax(self, axis=..., fill_value=..., out=..., *, keepdims=...): ...
468470
def sort(self, axis=..., kind=..., order=..., endwith=..., fill_value=..., *, stable=...): ...
469-
def min(self, axis=..., out=..., fill_value=..., keepdims=...): ...
471+
@overload
472+
def min( # type: ignore[override]
473+
self: _MaskedArray[_SCT],
474+
axis: None = None,
475+
out: None = None,
476+
fill_value: _ScalarLike_co | None = None,
477+
keepdims: Literal[False] | _NoValueType = ...,
478+
) -> _SCT: ...
479+
@overload
480+
def min( # type: ignore[override]
481+
self,
482+
axis: _ShapeLike | None = None,
483+
out: None = None,
484+
fill_value: _ScalarLike_co | None = None,
485+
keepdims: bool | _NoValueType = ...
486+
) -> Any: ...
487+
@overload
488+
def min( # type: ignore[override]
489+
self,
490+
axis: None,
491+
out: _ArrayType,
492+
fill_value: _ScalarLike_co | None = None,
493+
keepdims: bool | _NoValueType = ...,
494+
) -> _ArrayType: ...
495+
@overload
496+
def min( # type: ignore[override]
497+
self,
498+
axis: _ShapeLike | None = None,
499+
*,
500+
out: _ArrayType,
501+
fill_value: _ScalarLike_co | None = None,
502+
keepdims: bool | _NoValueType = ...,
503+
) -> _ArrayType: ...
470504
def max(self, axis=..., out=..., fill_value=..., keepdims=...): ...
471505
def ptp(self, axis=..., out=..., fill_value=..., keepdims=...): ...
472506
def partition(self, *args, **kwargs): ...

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ assert_type(np.ma.min(MAR_f4, keepdims=True), Any)
3232
assert_type(np.ma.min(MAR_f4, out=MAR_subclass), MaskedNDArraySubclass)
3333
assert_type(np.ma.min(MAR_f4, None, MAR_subclass), MaskedNDArraySubclass)
3434

35+
assert_type(MAR_b.min(), np.bool)
36+
assert_type(MAR_f4.min(), np.float32)
37+
assert_type(MAR_b.min(axis=0), Any)
38+
assert_type(MAR_f4.min(axis=0), Any)
39+
assert_type(MAR_b.min(keepdims=True), Any)
40+
assert_type(MAR_f4.min(keepdims=True), Any)
41+
assert_type(MAR_f4.min(out=MAR_subclass), MaskedNDArraySubclass)
42+
assert_type(MAR_f4.min(None, MAR_subclass), MaskedNDArraySubclass)
43+
3544
assert_type(np.ma.max(MAR_b), np.bool)
3645
assert_type(np.ma.max(MAR_f4), np.float32)
3746
assert_type(np.ma.max(MAR_b, axis=0), Any)

0 commit comments

Comments
 (0)
0